Monday, June 19, 2017

IntelliJ ctrl-click references a .class file not a .java file

Leave a Comment

I have the following line of code in a .groovy file for testing:

GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch(); 

Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.

Any help would be greatly appreciated!

3 Answers

Answers 1

First guess - Wrong import

What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.

Eg. annotation class Service is wildly used in different packages.

import org.springframework.stereotype.Service; import com.google.web.bindery.requestfactory.shared.Service; 

Just check that you are using the correct import.

Second guess - Incorrectly set modules

If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.

In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.

In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.


Basics about classes

Local class

Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.

Linked class (from external library)

If you have imported external library it WILL link to .class as it is decompiled from .jar file.

What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.

Answers 2

Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.

Answers 3

You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin

It allows you to display all the Java sources during your debugging process, even if you do not have them all

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment