可以在线浏览 OpenJDK 的源代码吗?

是否可以在线浏览 OpenJDK 的源代码,就像我可以在 SourceForge 的项目中做的那样?我以前从没用过水银,所以我感到很困惑。

(注意: 我不想下载源代码。我只是想在线浏览一下,看看一些方法是如何实现的。)

28357 次浏览

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?

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

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/

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

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

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 ABC3 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

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.

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