70

I cannot for the life of me attach the java source code to eclipse so I can see the inner workings of the language. Not even something as simple as the String Class.

when I run java -version this is what I have:

java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)

I am downloading the java souce from: http://download.java.net/jdk6/source/

And in eclipse when I attach it It says:

The JAR file "C:\Program Files\Java\jre6\jdk-6u14-fcs-src-b08-jrl-21_may_2009.jar" 
has no source attachment.

What am I doing wrong?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
hello_world_infinity
  • 4,760
  • 9
  • 32
  • 32

14 Answers14

84

Normally, if you have installed the JDK6u14, eclipse should detect it and declare it automatically in its "installed JRE" list.

If not, you can add that JDK through "Windows/Preferences": Java > Installed JREs:

Just point to the root directory of your JDK installation: it should include the sources of the JDK (src.zip), automatically detected and attached to rt.jar by eclipse.

Add JRE VM, from pic.dhe.ibm.com/infocenter/iadthelp/v8r0/topic/org.eclipse.jdt.doc.user/tasks/images/task-add_jre_std_vm.PNG

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • E:\Program Files\Java\jre6 -> Java Home E:\Sun\SDK\jdk -> Path Variable I have these two folders. Which one should I point to what?!? Is there a difference between the JDK and SDK? I because I did a search on my computer and I can't find src.zip. I have rt.jar in both folder but I don't know what to do with it... – hello_world_infinity Jul 21 '09 at 07:11
  • 3
    Under XP I have never seen Eclipse picking up the JDK - only the public JRE installed with it. – Thorbjørn Ravn Andersen Jul 21 '09 at 07:47
  • @ Thorbjørn Ravn Andersen : eclipse will detect it if you specify the vm to use when launching Eclipse in the eclipse.ini – VonC Jul 21 '09 at 08:24
  • The problem is that Sun's source code is not the same as Android's. Any idea how to find the exact source code on Google's devices (Java/JDK API)? – AlikElzin-kilaka Jul 11 '11 at 18:49
  • thanks but i could not find the source code of the valueOf method for enum with this method – necromancer Apr 27 '13 at 10:29
31

You don't necessarily need to add the source, but you rather may need to remove a JRE that does not have the source attached.

On looking at the "installed JRE's" I saw that my JDK was setup properly with source, but the default JRE on the machine had no sources. Eclipse was defaulting to that when looking for source.

I just used the remove button to expel the JRE, leaving my JDK. I then hit F3 and the source was there. Yeah!

Joseph Lust
  • 19,340
  • 7
  • 85
  • 83
24

Hold ctrl key and then click on class of which you want to see the inner working (for ex: String) then you will find there button "Attach Source". Click on it. Then click on External Folder. Then browse to your jdk location, per instance C:\Program Files\Java\jdk1.6.0. That's it.

enter image description here

Community
  • 1
  • 1
JAB
  • 3,546
  • 9
  • 36
  • 39
9

To attach JDK source so that you refer to Java Source Code for code look-up which helps in learning the library implementation and sometimes in debugging, all you have to do is:

In your Eclipse Java Project > JRE Reference Library locate rt.jar. Right click and go to Properties:

Select "Java Source Attachment" on the right and to the left select "External Location" and click on "External File" Button and locate "src.zip" file in your $JAVA_HOME path in my case for my windows machine src.zip location is: C:/Program Files/Java/jdk1.7.0_45/src.zip.

You are done now! Just Ctrl + click on any Java library Class in your project code to look-up the source code for the java class.

praveenak
  • 400
  • 5
  • 7
7

The easiest way to do this, is to install a JDK and tell Eclipse to use it as the default JRE. Use the default install.

(from memory)

Open Window -> Prefences. Select Installed Java runtimes, and choose Add. Navigate to root of your JDK (\Programs...\Java) and click Ok. Then select it to be the default JRE (checkmark).

After a workspace rebuild, you should have source attached to all JRE classes.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
5

Easy way that just worked for me:

  1. Check if you have a jdk installed (it is usually in "Program Files\Java", if you don't have one, install it.
  2. Link it to eclipse. In Preferences --> Java --> Installed JREs remove the JRE installed and add the JDK.

Then the project will detect it and add the new libraries from the JDK. At the end of this process it looks like the screenshot I attach.

JDK added

Weslor
  • 22,180
  • 2
  • 20
  • 31
3

Just remove the JRE in Preferences>Java>Installed JRE and add the folder of your JDK. If you just add JDK but still leave JRE it won't work

Thiago
  • 746
  • 1
  • 10
  • 22
2

Answer For Eclipse 2019 With ScreenShots

  • Step 1: Window -> Preferences -> Java -> Installed JREs

Installed JREs

  • Step 2: Select Currently used JRE, now Edit option will get enabled, Click on edit option

Currently used JRE  option edit

  • Step 3: Select rt.jar from JRE systems library, click on corresponding drop down to expand

rt.jar

  • Step 4: Select Source attachment none, Click on Source Attachment Button, Source attachment configuration window will appear, Select external location

Source attachment external location

  • Step 5: Select src.zip file from jdk folder, say ok ok finish

src.zip

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

What worked for me (with JDK7) was the following:

  1. Download the openjdk-xy-sources.jar from GrepCode,
  2. rename the file to src.zip,
  3. copy src.zip to the root folder of the JRE/JDK added to eclipse (the one with bin and lib folders in it)
  4. restart eclipse

Alternatively, if you don't want to write to your JDK folder, you could also attach src.zip to (at least) rt.jar in eclipse in the Window | Preferences menu in Java | Installed JREs.

If you're not comfortable with downloading the sources from GrepCode, you could also get them from openJDK directly. This requires requires a bit more effort, though. Replace step one above by the following steps:

  1. Download the JDK sources from here,
  2. extract all folders from the zip file's *openjdk\jdk\src\share\classes* folder,
  3. create a file src.zip and add these folders to it,
  4. copy src.zip to the root folder of the JRE/JDK added to eclipse (the one with bin and lib folders in it)
  5. restart eclipse

A third alternative to acquire src.zip is to download the unofficial OpenJDK builds from here. The src.zip is contained within the downloaded zip.

schnatterer
  • 7,525
  • 7
  • 61
  • 80
1

Old question, be here's what I ended up using Java Source Attatcher plugin: http://marketplace.eclipse.org/content/java-source-attacher#.U85j4rF-N7s

It can provide source for different opensource projects. One weird thing thou, if you have libraries under libs folder, you cannot attach from there, but you need to attach source clicking on jar displayed under "Libraries from external".

andro83
  • 3,563
  • 2
  • 18
  • 13
0

The option provided by"praveenak" can also be applied to any other jar files with source file provided. For example, for JavaFx, we right click jfxrt.jar, select "Properties" and enter jdk1.8.0_05/javafx-src.zip for "Path" under External location.

0

You need to attach java sources which comes with JDK(C:\Program Files\Java\jdk1.8.0_71\src.zip).

Steps(**Source: link):

  1. Select any Java project
  2. Expand Referenced libraries
  3. Select any JAR file, in our case rt.jar which is Java runtime
  4. Right click and go to properties
  5. Attach source code by browsing source path.
0

issue resolved here :

"Windows/Preferences": Java > Installed JREs:

then set/update JRE HOME : C:\Program Files\Java\jdk1.8.0_221\jre

should try this.

0

enter image description here

if you are add jre and jkd path, remove jre install path, keep jdk path is work