Thursday, April 14, 2011

Eclipse opens .class instead of .java

One neat feature of Eclipse (and other IDEs) is that you can hold CTRL and click on a function or variable to go to its definition.

A problem which may occur in Eclipse is that it opens the .class file (the compiled file) instead of opening the .java file when you do this. This is probably caused by the order of the entries in the .classpath file.

Go to your project directory and open the .classpath file. Move all entires of type classpathentry with attribute kind="src" to the top of the file (or at least above any entries with kind "output").

Restart Eclipse and the problem should be fixed.

4 comments:

  1. It's not working, all my scr kind entries is on the top. :(

    ReplyDelete
  2. @SaNNyO: Difficult to say why it might not work, here are some ideas:

    1. Are you sure you are editing the correct file? Make sure it's the one in the project you are looking at. :)
    You can apparently also do these changes by right-clicking your project in Eclipse, select Properties, select Java Build Path, select the Order and Export tab, and then move the folders to the top using the 'Up' button.

    2. A restart of Eclipse should catch any changes you do, but you might want to try to refresh the project too. Right-click the project and select 'Refresh'.

    3. Maybe you don't have the source for the class/method you are trying to open. This could happen if it is an added library (JAR) or part of Java Core. Check out http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/reference/ref-properties-source-attachment.htm to see if you can add the source to your JARs.


    Also see http://stackoverflow.com/questions/4035400/classpath-build-order-changes

    ReplyDelete
  3. you can make it using eclipse it self without restarting
    right click on your project -> java build path -> order and export ->(select the type src) -> top ->ok

    ReplyDelete
    Replies
    1. @Shrif your're right, that should do it. I've noticed however that sometimes Eclipse needs a good restart to pick up on the changes you do.

      Your way keeps one from having to mess around in the file system and find the correct lines in the classpath file.

      Notice that in the Order and Export dialog in Eclipse, the source items will have folder icons just like in the project tree itself in Eclipse. Makes it easier to find the source items.

      Delete