Is it possible to browse the source of OpenJDK online?
Asked Answered
D

8

87

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.)

Dendritic answered 4/1, 2009 at 9:41 Comment(0)
F
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

Faints answered 4/1, 2009 at 10:5 Comment(13)
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.Dendritic
You can find that one here: hg.openjdk.java.net/nio/nio/jdk/file/d5e2831c01c4/src/share/… (or perhaps see here: #411205 )Faints
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.Hoedown
if you want the JDK7 version, it's here: hg.openjdk.java.net/jdk7/jdk7/jdk/file/tip/src/share/classes/…Genesa
@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.Spondylitis
What does "gate" in the repo name mean?Uncover
@finnw: Very good question. I have no idea: consider asking a top-level question about it.Faints
Now if I want to see the native code that implements the native methods, where can I find them?Fenske
@Ustaman Sangat: Look under src/windows/native, src/linux/native or src/solaris/native. For instance hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/tip/src/windows/…Faints
@RasmusFaber Thanks. Now a dumber question, I was looking for Object.hashcode() implementation, can't figure it out from any files at hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/6b46f3c7c97c/src/…Fenske
@Ustaman Sangat: Ah, sorry. Non-platform-specific methods are in src/share/native. java.lang.Object is in hg.openjdk.java.net/jdk6/jdk6-gate/jdk/file/tip/src/share/… . You will notice that it delegates hashCode() to JVM_IHashCode() which is in hg.openjdk.java.net/jdk6/jdk6-gate/hotspot/file/tip/src/share/… . This again calls FastHashCode() in hg.openjdk.java.net/jdk6/jdk6-gate/hotspot/file/tip/src/share/… , which will end up calling get_next_hash() in the same file.Faints
But if need to clone the jdk7 latest repo, what is the url?Mirilla
FYI, the Java 8 version of that example List class source file.Ullrich
F
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/

Firenze answered 20/7, 2013 at 12:25 Comment(0)
P
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?

Piet answered 4/1, 2009 at 9:48 Comment(2)
Not exactly. I went there before I asked but I couldn't find my way around the Mercurial interface. :(Dendritic
Sorry, I actually went to openjdk.java.net/projects/nio and tried to find my way from there. I'll check this link again. Thanks.Dendritic
H
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
Haemophilia answered 18/3, 2016 at 8:13 Comment(0)
I
6

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

Intumescence answered 12/9, 2013 at 22:40 Comment(0)
E
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

E answered 16/9, 2014 at 17:4 Comment(0)
A
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

Anesthesiologist answered 20/4, 2019 at 14:10 Comment(0)
W
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.

Woke answered 27/7, 2018 at 20:42 Comment(1)
zgrepcode looks like a click baiter. Lots of popups and ads showed up.Shinto

© 2022 - 2024 — McMap. All rights reserved.