86

Is it possible to browse the source code of OpenJDK online, just like I can do with SourceForge's projects? I never used Mercury before, so I felt confused.

(Note: I don't want to download the source. I just want to browse it online, to see how some methods are implemented.)

dbr
  • 165,801
  • 69
  • 278
  • 343
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182

8 Answers8

100

OpenJDK is now on GitHub: https://github.com/openjdk/jdk

It is a large project, but you will find the implementations of the core classes under jdk/src/java.base/share/classes.

For instance you can find the implementation of java.util.List here.


If you need to browse older versions, you still need to use the old Mercurial interface.

The Mercurial interface there is quite confusing if you are not used to it, and since this is a large project, it can be hard to find what you are looking for.

Here is an example:

To find the JDK6 implementation java.util.List, select jdk6, jdk, select browse. Then browse to src/share/classes/java/util/List.java.

You should end up at http://hg.openjdk.java.net/jdk6/jdk6/jdk/file/tip/src/share/classes/java/util/List.java

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
  • Thanks a lot. I went there trying to find the source for java.nio.file.FileRef, but I couldn't find it. Could you please help me? Thank you. – Hosam Aly Jan 04 '09 at 10:20
  • You can find that one here: http://hg.openjdk.java.net/nio/nio/jdk/file/d5e2831c01c4/src/share/classes/java/nio/file/FileRef.java (or perhaps see here: http://stackoverflow.com/questions/410705/best-way-to-determine-if-two-path-reference-to-same-file-in-c#410794 ) – Rasmus Faber Jan 04 '09 at 10:41
  • 2
    For anyone trying this the default branch number changes. So the procedure described is correct, just don't be surprised if the link doesn't work. – Patrick Feb 10 '09 at 18:09
  • 5
    if you want the JDK7 version, it's here: http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/java/nio/file/FileRef.java – user102008 Feb 01 '11 at 22:04
  • 3
    @Patrick: if you replace the changeset ID with 'tip', it will always work. The version it points to may change over time, but it will always be the most up-to-date (in some sense). I've edited the post accordingly. – Tom Anderson Apr 05 '11 at 08:47
  • 4
    What does "gate" in the repo name mean? – finnw May 26 '11 at 18:06
  • @finnw: Very good question. I have no idea: consider asking a top-level question about it. – Rasmus Faber May 26 '11 at 18:29
  • 2
    Now if I want to see the native code that implements the native methods, where can I find them? – Ustaman Sangat Dec 14 '11 at 15:54
  • 1
    @Ustaman Sangat: Look under src/windows/native, src/linux/native or src/solaris/native. For instance http://hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/tip/src/windows/native/com/sun/security/auth/module/nt.c – Rasmus Faber Dec 14 '11 at 16:46
  • @RasmusFaber Thanks. Now a dumber question, I was looking for Object.hashcode() implementation, can't figure it out from any files at http://hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/6b46f3c7c97c/src/windows/native/java/lang/ – Ustaman Sangat Dec 14 '11 at 18:32
  • @Ustaman Sangat: Ah, sorry. Non-platform-specific methods are in src/share/native. java.lang.Object is in http://hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/tip/src/share/native/java/lang/Object.c . You will notice that it delegates hashCode() to JVM_IHashCode() which is in http://hg.openjdk.java.net/jdk6/jdk6-gate/hotspot/file/tip/src/share/vm/prims/jvm.cpp . This again calls FastHashCode() in http://hg.openjdk.java.net/jdk6/jdk6-gate/hotspot/file/tip/src/share/vm/runtime/synchronizer.cpp , which will end up calling get_next_hash() in the same file. – Rasmus Faber Dec 14 '11 at 19:42
  • But if need to clone the jdk7 latest repo, what is the url? – St.Antario Sep 29 '15 at 12:26
  • FYI, the Java 8 version of that example [`List`](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/List.java) class source file. – Basil Bourque Apr 01 '16 at 05:22
9

The latest JDK 8 OpenJDK Java Class Library source code can be found here: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/tip/src/share/classes/

anishpatel
  • 1,472
  • 1
  • 16
  • 23
Abdull
  • 26,371
  • 26
  • 130
  • 172
7

Here is the basic step to get latest or any released version of Openjdk 8 (or any existing java version) source code, and use them in Eclipse.

Steps:

  • [browse source]
  • Open url for jdk, e.g http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/
  • click tags
  • choose proper tag, e.g jdk8u73-b02
  • then click browse,
  • then browse into folder src/share/classes,
  • [download source]
  • then click one of bz2 / zip / gz, to download source in relevant compressed format, (e.g for jdk8u73-b02 in zip format, the url will be: http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/archive/2ab13901d6f1.zip/src/share/classes/)
  • [use in eclipse]
  • uncompress it,
  • zip the folder "classes/", make "classes/" as the root dir of .zip file, (e.g first cd jdk-2ab13901d6f1/src/share/, then zip -r openjdk_8u73_b2_src.zip classes/)
  • move the created zip file to proper location, it will stay there for a while, (e.g mv openjdk_8u73_b2_src.zip /media/Eric/software/java/jdk/openjdk/openjdk8u73-b02/source/)
  • in eclipse, specify source file for jars of installed jre, could specify the source attachment for each jar of installed jre respectively, the most common jar is probably rt.jar,
  • optionally, might need refresh project to make it totally take effect, not sure is that necessary,
  • test it: in eclipse, ctrl + shift + t, then input Cancellable, select the sun.nio.fs.Cancellable of corresponding installed jre, if the source code is available, then it's good, because this source is not available in jdk_home/src.zip, it must be from the additional openjdk source,
  • switch source back: could switch back to use "jdk_home/src.zip", if don't want to use the external openjdk source,
  • ok
Eric
  • 22,183
  • 20
  • 145
  • 196
7

Here's a way to browse the repositories and look at just the bits you want. http://hg.openjdk.java.net/ Is that what you are asking?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • 1
    Not exactly. I went there before I asked but I couldn't find my way around the Mercurial interface. :( – Hosam Aly Jan 04 '09 at 10:11
  • Sorry, I actually went to http://openjdk.java.net/projects/nio and tried to find my way from there. I'll check this link again. Thanks. – Hosam Aly Jan 04 '09 at 10:15
6

Append a "/file" to the root URLs to view the browser like this:

Ashwin Jayaprakash
  • 2,168
  • 24
  • 29
2

Grepcode.com is great for similar things - not only OpenJDK sources, with searching in classes/methods and links between classes directly in highlighted code:

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/java/net/Socket.java

Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
Honza
  • 974
  • 10
  • 18
2

As mentioned in the other answers, the source code repository is at https://hg.openjdk.java.net

However, the OpenJDK team mirrors some of the projects on GitHub: https://github.com/openjdk

Including the latest Java version project (https://hg.openjdk.java.net/jdk/jdk): https://github.com/openjdk/jdk

Marcono1234
  • 5,856
  • 1
  • 25
  • 43
1

Surely http://hg.openjdk.java.net is one good option. The other equally good source is zGrepCode https://zgrepcode.com/java/openjdk/ . It has both Open JDK and Oracle java versions.

Arry
  • 1,630
  • 9
  • 27
  • 46