'Eclipse Java Project - Open Declaration always opens class file instead of .java

I'm posting this query after doing lots of googling and trying out different options for last few days. Is there an easy way to attach source in Eclipse? is the closest answer I found to my question.

From above post, I tried all solutions but each time Eclipse is opening .class file instead of .java file.

Eclipse Details:

  • Kepler Service Release 2
  • Build id:20140224-0627

Please let me know if this is a bug with Eclipse or if I am missing something.



Solution 1:[1]

Maven is likely supplying a Classpath Container to your project, and generating entries for the Java Build Path based on the pom.xml contents (the JRE System Library is another example of this). Its entries will not have source attachment unless the container decides it will, which is why the Installed JREs preference page offers to let you set Source Attachments. If the pom.xml has something like that, see if you can use it. If not, see if you can configure the Maven container from its context menu. Otherwise, you may simply be stuck.

Solution 2:[2]

If you are using Maven, try this:

Double click on your project in eclipse -> Maven -> Enable Workspace Resolution

Solution 3:[3]

This is most likely because Eclipse actually use the class file for declaring the item you have requested the declaration for, and not the source file you think it should be using.

The typical reason for this is if you have a jar file containing previously compiled classes on your build path before (or instead) of the project containing the source you want. This is very rarely desirable as it can take quite a while to discover that your edits do not take effect.

Examine your build path carefully and ensure it is as you want it to be.

Solution 4:[4]

I had this same problem: when I pressed F3 or tried to open Declaration (after right click) for any element, it took me to the bytecode .class file. My solution:

I went to the project in Project Explorer:

right click on project name->Properties

Then in Porject Properties window,

Java Build Path->"Order and Export" tab

There I selected the folder with the .java files (source code), which was below the folder having the .class files -in my case "JRE System Library [java-11-openjdk-amd64]", and pressed 'Top' button on the right frame. Then the folder with source files moved to the top.

Finally, pressed "Apply and Close" button.

That solved my problem forever and ever.

The folder

Solution 5:[5]

I installed a plug-in which allows me to download the sources for a maven project. I'm not by my develop machine right now, but I think it was this:

http://marketplace.eclipse.org/content/m2e-dynamic-sources-lookup#.U8gjGWIaySM

With this, you can select a project an have maven download the sources and make then available when you navigate the code in Eclipse. All without "leaving" Eclipse.

Solution 6:[6]

Check into your project modules the Java Build Path, look inside the Source tab and verify if you have "Allow output folders for source folders" checkbox flagged; in this case you just need to uncheck it and you will be able to see again the right source from java files.

Solution 7:[7]

This is related to the Call Hierarchy in Eclipse IDE.

If you are in a situation, where you're seeing duplicate methods inside the call hierarchy showing Members calling 'myMethod(...)', double-clicking on one of the listed methods may open the .class file instead of the .java file.

When this happens, the solution is to set the search scope in the call hierarchy to Project instead of Workspace.

enter image description here

After setting the search scope, you won't be seeing duplicate methods listed in the call hierarchy, and double-clicking on the listed methods will only open the .java files.

(Using Eclipse DevStyle Dark Theme)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nitind
Solution 2 rfvallina
Solution 3 Thorbjørn Ravn Andersen
Solution 4 rodolk
Solution 5 fredrik
Solution 6 Donatello Boccaforno
Solution 7 tom_mai78101