tags:

views:

3555

answers:

7

Using Eclipse I want to view the source code for a core Java class (E.g. java.util.concurrent.ConcurrentHashMap) but when I navigate to the source using 'Open Declaration' it says 'Source not found' and gives me the option to attach the source.

My question is; how do i attach the source? Where do i get the source .jar from for the java.util.concurrent library?

+2  A: 

You can go to http://openjdk.java.net/ and download the latest builds of the openJDK project. I think this should give you what you need.

Brian Fisher
+10  A: 

You need to have the JDK installed. Then you can look in JDK_INSTALL_DIR\src.zip

For me it is C:\Program Files\java\jdk1.6.0_11\ (depends on your current version)

You don't need to get a special open source version.

jjnguy
+4  A: 

There are a few good answers here on where to get the source. But a word of caution: I'd be wary about how you use it (if you're using it simply for reference). The API documentation is the only contract you should code against, and is what the developers will keep consistent/intact between releases. I wouldn't use the source find out implementation details and then code my applications with regard to those implementation details, as they may change between releases.

Rob Hruska
i just want to see the code - to see how it works
No problems with that :)
Rob Hruska
+2  A: 

If you can't find the actual source you can also use a decompiler to regenerate source from the class file.

Personally I use JAD combined with the JADClipse plugin to view source in Eclipse.

Michael Rutherfurd
Michael, thanks! JAD and JadClipse are a great find.
Domchi
A: 

You should be able to see "JRE System Library [jdk1.x.xxxx]" when you look at your project's Java Build Path.

You can access the project build path configuration screen by: right clicking on the project -> Build Path -> Configure Build Path... You should be able to see the JRE System Library entry at the bottom of the list.

The easiest way to view the source for the class is to use the "Open Type" shortcut. The default for this shortcut is: "Ctrl + Shift + T". The class you're looking for should appear as you type it's name.

A: 

When you are coding in Eclipse, press CTRL and click on any core Java class name in your source. Eclipse will now show a screen saying you don't have the sources installed. However, in this screen there is a link saying "Attach source...". Click that link and import the src.zip file from your JDK installation directory (src.zip). This should do the trick

Chris Velpe
A: 

There is a good plugin GrepCode which allows viewing of java sources for many open source libraries.

Dmitry Sobolev