Where can I find the Java JDK source code? [closed]
Asked Answered
I

11

168

I would like to see what a method in the Java API does. So I want the JDK source code.

Before I reinstalled Linux I had the src.zip package with all the official source code in it. I just had to tell Eclipse where this file is and I could see the code. But now I don't have the file anymore...

Where can I find it?

Iila answered 24/5, 2010 at 11:51 Comment(13)
I've always found the src.zip file that contains the public Java API in the JDK installation directory. Is that what you're looking for (I've mainly used Windows, so maybe the Linux distro doesn't have it).Restate
This is riddiculous that this question is #3 in google when searching for JDK source. #1 and #2 are Oracle links that do NOT have source code links. I always go through this question when installing JDK. Oracle please.Sentence
src.zip is the source bundle for core API only. There are no publicly available sources for the entire Java SE from Oracle, some portions are closed source. However, depending on a platform, you can install and use on OpenJDK binary with 100% sources available. For sources - head over to openjdk.java.net and clone a repository of sources for the version that you need. For binaries - a few vendors provide such binaries including IcedTea for linux.Qualify
@Hoto, I've been using Java since 1996 and I am pulling my hair out trying to find the oracle src.zip for JDK7. This is unbelievably stupid. WhereTF is it???? It never used to be this difficult. Note, I don't want OpenJDK.Chihuahua
@Ash, I used to always find a src.zip in the installation directory as well. You would then extract it to a ./src/ directory, and all was well. But it's not there now. IN FACT, the JDK 1.7 readme indicates that there should be a src.zip present. And there isn't. This is maddening.Chihuahua
Why does this question have so many upvotes?Bionics
@HenryIsVeryPro: It's from 2010. I guess that 5 years ago, the source was hard to find for some reason. Haven't searched for the source recently. It tends to be always included when I install Java nowadays.Iila
Actually @MartijnCourteaux, this problem manifested itself again in 2015 with the Windows release. I had a few theories regarding it, but they seem to have put it back. However, before they did I managed to find it safe and sound in the linux release, as indicated in my answer below.Chihuahua
Is there anybody tried to search the folder /usr/lib/jvm? I found the src.zip under /usr/lib/jvm/jdk-8-oracle-x64/ in my machine.Goulash
@HenryZhu Because it's a good question about a problem that is wasting tons of time to a lot of people, I guess. You forgot to say - or hint to - why you find it strange.Seat
For jdk 9 it's moved to under jdk/lib/src.zipLumpy
Why has this question been closed? It certainly is a “a practical, answerable problem that is unique to software development” (stackoverflow.com/help/on-topic), and it does not “fit into one of the categories [which] may be closed by the community”. Admittedly, it is too vague as it should specify OpenJDK or Oracle JDK, but this is easily solved. And it would deserve an up-to-date answer referring to the OpenJDK GitHub repositorySapper
How can such an interesting question be marked as "closed" and "not following SO guidelines" is beyond my understanding. This question has value for the community.Fleam
P
128

You haven't said which version you want, but an archive of the JDK 8 source code can be downloaded here, along with JDK 7 and JDK 6.

Additionally you can browse or clone the Mercurial repositories: 8, 7, 6.

Photoelectrotype answered 24/5, 2010 at 11:53 Comment(15)
Yes; lets say I want 6 and then on that download page, what to I have do download? The big file jdk-6u21-ea-src-b04-jrl-05_may_2010.jar, 136.48 MB? I already downloaded that one. But doesn't contain the source code.Iila
@Martijn: Did you try downloading the big jar file and then reading the README inside it? Basically you just run it with java -jar jdk-6u21-ea-src-b04-jrl-05_may_2010.jar. It will extract the source.Photoelectrotype
See also #2803998Blinny
Yes, I did it. And I extracted it like you wrote. But there is no source jar or zip in it.Iila
@Martijn: You don't extract the jar file - you execute it. That then extracts the source to whichever directory you want. You don't end up with a zip or jar file - you end up with the source in your file system. You can then tell Eclipse to use that.Photoelectrotype
@JonSkeet: Do you happen to know where to get "unofficial" sources for com.sun.* packages for JDK 1.6?Christine
@dma_k: Not offhand, no.Photoelectrotype
Just wanted to add that the current (2013) place is now jdk7.java.net which redirects to OTN for any downloads.Worsen
Can someone point me to how to access the source code from the JDK 7 file linked in this post? I downloaded it but the README didn't help -- I don't know anything about Mercurial -- and I'm just looking for the plain text .java files.Myra
For 1.7 there is also sourceforge.net/projects/jdk7srcNeptune
Those links are for OpenJDK, and do not apply if you have Oracle's, Apple's, IBM's, GNU's, etc JDK.Yes
I downloaded the zip file for JDK 8 (~ 118 MB), and it oddly does not contain the source for java.nio.ByteBuffer. However, I followed @amertkara's approach, got a 43 MB ZIP file, which included the aforementioned file.Extol
@JonSkeet, Is this the exact same source code used to power Oracle's JDK-JRE and Oracle's Server-JRE? oracle.com/technetwork/java/javase/downloads/index.htmlMessage
@Pacerier: I don't know, to be honest. I'd expect the OpenJDK to have some differences in the JRE, but I suspect the Java code is the same.Photoelectrotype
For jdk 9 it's moved to under jdk/lib/src.zipLumpy
S
39

Chances that you already got the source code with the JDK, is a matter of finding where it is. In that case, the JDK folder doesn't contain the source code:

sudo apt-get install openjdk-7-source

OS X folks, search in Homebrew formulas.

In Ubuntu, the command above would put your source file under:

/usr/lib/jvm/openjdk-7/

The good news is that Eclipse will take you there already (how to bind Eclipse to the Java source code):

Follow the orange buttons

Enter image description here

Enter image description here

Enter image description here

Subteen answered 24/4, 2015 at 15:7 Comment(0)
C
18

Sadly, as of this writing, despite their own documentation readme, there is no src.zip in the JDK 7 or 8 install directories when you download the Windows version.

Note: perhaps this happens because many of us don't actually run the install .exe, but instead extract it. Many of us don't run the Java install (the full blown Windows install) for security reasons...we just want the JDK put someplace out of the way where potential viruses cannot find it.

But their policy regarding the Windows .exe (whatever it truly is) is indeed nuts, however, the src.zip DOES exist in the Linux install (a .tar.gz). There are multiple ways of extracting a .tar and a .gz, and I prefer the free "7Zip" utility.

  1. download the Linux 64 bit .tar.gz
  2. use 7-Zip to uncompress the .tar.gz to a .tar
  3. use 7-Zip to extract the .tar to the installation directory
  4. File src.zip will be waiting for you in that installation directory.
  5. pull it out and place it where you like.

Oracle, this is really beyond stupid.

Chihuahua answered 1/5, 2015 at 23:56 Comment(3)
Oracle JDK8 (1.8.60 at least) had the src.zip for me on Windows. Eclipse didn't pick it up by default however.Rb
@milletron, I too noticed it in a recent windows release. I have a feeling that something broke down in their windows download image production for some period of time previously. Perhaps related to the difference in line termination b/w unix (LF) and windows (CRLF)?Chihuahua
This is what I had to do also for Java 9.Abrams
M
11

I had this problem with my Ubuntu.

All I needed to do to get sources for my Java installation was:

sudo apt-get install sun-java6-source
Metralgia answered 27/2, 2011 at 13:56 Comment(1)
On Ubuntu 14.04: E: Unable to locate package sun-java6-sourceSindhi
C
9

The JDK 1.6 I'm currently using on OS X v10.8 (Mountain Lion) did not come with a src.zip either, and as far as I can tell there is no supported Mac OS X JDK for 1.6 available anymore.

So I downloaded the OpenJDK source (using the links from the accepted answer (+1)) then ran:

cd ~/Downloads
mkdir jdk6src
cd jdk6src
tar xf ../openjdk-6-src-b27-26_oct_2012.tar.gz
cd jdk/src/share/classes
jar cf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/src.jar *

(Your file names and paths may vary...)

Associate that src.jar with the appropriate Java platform in your IDE and you should be good to go.

There are some discrepancies between the OpenJDK source and the JDK I'm currently running (line numbers don't match up in the debugger, for one), but if all you want is a zip/jar to point your IDE to for the relatively few cases you need to peek at some code to understand how something works, then this should do the trick.

Charliecharline answered 16/10, 2013 at 15:52 Comment(3)
thx, i needed this while using mac. there's like no clear instructions regarding the latest OS and how to grab the source. (they all talk about downloading updates which don't include the source or downloading from oracle but it's all vague or erroneous in one way or another)Whitley
Just wanted to highlight that this is the right way to do it, because like @Whitley said, every other instruction out there is erroneous or stale. Did this with IntelliJ 15 on a Scala project, it worked perfectly. Thanks @MisterEd!Urogenous
src.jar is not a complete source. For ex, it doesn't contain the source of javax.netNever
M
4

This file is contained in the standard JDK download. Also your Linux system probably has the JDK in the repository. In my Ubuntu Linux, the file is located in /usr/lib/jvm/java-6-sun-1.6.0.20/src.zip.

Mantelletta answered 24/5, 2010 at 12:22 Comment(1)
Aha! Can you post which packages you have installed for your Java VM and JDK, please. Because I don't have that folder.Iila
T
4

In JDK 8, the source can be found in <JDK installation directory>/src.zip. Now in some intermediate releases this ZIP file was missing, but again it is available.

Enter image description here

Make sure that you select the source as well from the installation wizard.

Typesetting answered 17/11, 2016 at 17:48 Comment(0)
R
2

Here the official link for the JDK source: http://www.oracle.com/technetwork/java/javase/downloads/index.html

(You may need to scroll to the bottom of the page.)

Rigmarole answered 31/1, 2012 at 7:30 Comment(0)
I
2

Well, I opened terminal in my Mac and typed:

echo $JAVA_HOME

Then I got the directory, went there and found src.zip.

Impure answered 2/5, 2015 at 5:48 Comment(0)
I
1

Yes!! I got it!

I downloaded the Java Developer Kit (JDK) from sun.com for Linux. There was file src.zip in. But first I uninstalled all Java packages with Synaptic.

Iila answered 24/5, 2010 at 14:8 Comment(1)
note that under Linux there's really no need to be root to install Java. I always (since more than ten years) install Java as a non-root user on my Un*x systems (so, no, I don't use apt-get / synaptic / whatever).Pitchy
P
1

The official link no longer offers the original source code. The official link and casual Google searches will land you with OpenJDK.

OpenJDK causes problems with an Android build unless the build script files are modified. The original package can be found here:

sudo add-apt-repository "deb http://ppa.launchpad.net/ferramroberto/java/ubuntu oneiric main"

This repository still has the sun-java6-source package.

Credit: Installing sun-java-6 with apt-get in Linux

Punnet answered 24/5, 2013 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.