0

By source code , i mean the actual .java files packed up inside src.zip located in the jdk folder. Should i Import the entire thing into eclipse and see from there ?

If so , then How ? Or is it done some other way ?

edit 2 :

Jeremy's suggestion worked best. Only the ctrl key has to be pressed before taking the pointer to the class you want to see the code for.

edit 1: From the suggestions i tried to import src.zip into eclipse by going into preferences. this is a snapshot of what i'm getting :

enter image description here

Its a major pain to search through because of the length of the list . I'm surely doing something wrong here ?

Somjit
  • 2,503
  • 5
  • 33
  • 60
  • 2
    If you're talking about the Java API...[look it up online.](http://docs.oracle.com/javase/7/docs/api/) You'll go through a **lot** less pain. – Makoto Sep 21 '13 at 04:52
  • online doc is better , the IDE provides doc view too when using methods – Srinath Ganesh Sep 21 '13 at 04:53
  • Umm , no , i mean the actual _code_ of the stuff. – Somjit Sep 21 '13 at 04:53
  • possible duplicate of [view java source code](http://stackoverflow.com/questions/6288377/view-java-source-code) – jamesSampica Sep 21 '13 at 05:03
  • May be not directly related, but have you tried IntelliJ community edition? If not, I'd suggest trying it. It takes care of a lot of these automatically for you. – KumarM Sep 21 '13 at 05:03
  • 1
    There's no single source, it depends on the Java implementation, and there are many different implementations. For example, there's [GNU's implementation](http://gcc.gnu.org/java/) and [Android](http://source.android.com/), to name two. – Adam Rosenfield Sep 21 '13 at 05:05

3 Answers3

2

If your are using Eclipse IDE you can set the src using Source Attachment dialog. Follow the below procedure

In Eclipse, Select a JAR in the Package Explorer and choose

Properties > Java Source Attachment from the context menu or the Project menu

Java src Then you can browse the source code just like how you browse your own code.

If your are looking up to set the Java SDK source. You should follow the steps here.

Community
  • 1
  • 1
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • I was trying out what you said before you made the edit. Can you explain how this is different from your 1st suggestion ? – Somjit Sep 21 '13 at 05:16
  • First I misunderstood that you wanted to attach JAVA SDK src to eclipse. Please let me know if that's what you are trying to do. – Jayamohan Sep 21 '13 at 05:22
  • As an example , i want to read the source for `ArrayList` in Java 7. the src.zip has it , and i want to read it . If you could see my updated post , i'm posting a picture of what i'm getting. – Somjit Sep 21 '13 at 05:36
2

You mentioned Eclipse.

Go to Preferences. In the left-hand menu, open Java and select Installed JREs. Select your JRE in the main pane and "Edit...". Find and select rt.jar. Click "Source Attachment...", "External File..." and navigate to src.zip. You might also want to set the Javadoc location to an appropriate location such as http://download.oracle.com/javase/7/docs/api/ (as a Javadoc URL).

Having done that, ctrl-click (or cmd-click on MacOS) will take you to the source code. You can also navigate directly to the source code by opening the JRE System Library under your project in the Package Explorer, and then opening rt.jar. (If source code isn't attached, you can still do that but when you try to open a class file or method/field therein, you'll end up seeing the Java byte code instead of the Java source.)

Don't forget, as commentators have said, that the API (Javadoc) is generally a better place to look than the source code. The source code can change; the API should not.

Jeremy
  • 326
  • 1
  • 5
  • So far , I'm getting a HUGE list of all the things in there. And its really painful to search through them. Also , where exactly do i ctrl + click ? – Somjit Sep 21 '13 at 05:58
  • Nevermind , Figured that i had to 1st hit ctrl then drag the pointer to the stuff i want to see the code of. Problem solved :) – Somjit Sep 21 '13 at 06:11
  • @SomjitNag you should learn at least the basic shortcuts and capabilities of your ide - it will save you hours (literally). – assylias Sep 21 '13 at 06:24
  • Very true. I'll be looking more into eclipse's bells and whistles now on . Thanks for all the help. :) – Somjit Sep 21 '13 at 07:29
1

You can see the JDK (OpenJDK) source code online, using a service like grepcode.com.

You can use grepcode's search function or simple google a source file by adding the site's name before a class, for example grepcode java.lang.String.

OpenJDK 7 is the Java SE 7 reference implementation. You can't get the reference implementation for Java 6 this way because of it's license, but if you are interested in how things work in general, the OpenJDK 6 will be fine too.

kapex
  • 28,903
  • 6
  • 107
  • 121
  • I went there , typed in `ArrayList` , and it showed me a whole bunch of options. What do i do if i just want to see the code for jdk 7's version of ArrayList ? – Somjit Sep 21 '13 at 05:33
  • @assylias : Yes , but while that's true , my question was how to go about viewing it. I can open things up individually in notepad and see them too , but I was hoping for something more eye-friendly , and easily searchable. – Somjit Sep 21 '13 at 05:50
  • 1
    @SomjitNag The search results show matching class names, but there often are multiple projects with different implementations of the class. See the top search result, "openjdk" is the one you'll want, next to it are the links to the source for the versions 6 and 7. – kapex Sep 21 '13 at 06:05
  • @SomjitNag what I meant is that you can view the jdk code directly in your ide so there is no need to search on grepcode or openjdk websites. – assylias Sep 21 '13 at 06:21