Search code examples
javaidesource-control-bindings

How to rebind JDK Source Code to my IDE?


I would like to see what a method in the Java API does. To get this, I need the JDK Source Code installed or placed on my computer somehow.

Before I re-installed Linux I had a src.zip package with all the official source code in it. I just had to tell my IDE where this file is and it would automatically find and display the Java code I was wanting to see. But now I have lost the the file and have not been able to relocate it. It would seem that Oracle has either hidden the source code or come up with a different way of publishing it.

I use Eclipse, but I think the procedure would be practically the same for most modern IDE's.

So the question is: How to I get my IDE to be able to look up and bind to the Java Source code again?


Solution

  • I wrote this question to be a modified duplicate of Where to find Java JDK Source Code?. That was closed because of being off topic, but I think this is a very relevant question.

    Short answers

    If you're working with Oracle's Java, all you need to do is install a JDK of the Java package you are working with, and then set up your IDE to use that JDK to support its projects. The "src.zip" is prepackaged, and your IDE (or at least Eclipse) should be able to display the source files in it on its own.

    If you are working with the open JDK, all you need to know is that the JDK, its Javadocs, and its source code are each in their own rpm. You to get the source code for the java version you're working with, you merely need to find and install the java-1.X.X-openjkd-src. 1.8 adds "-debug" to the name. Once you've done that, just restart your IDE, and it should pick up the source files while reloading its projects.

    Oracle

    If you are working with Oracle's Java, you need to make sure you are developing against a JDK, not a JRE. Here is an example site, which allows you to download the JDK for JavaSE: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

    You'll want to accept the license agreement and then pull the appropriate JDK for the system you want to install it on. In my case, I'm installing on 64 bit Linux, so I downloaded jdk-8u121-linux-x64.rpm.

    Once downloaded, run the installer. That done, look for where the installer actually put its files. On Linux, you can use this command to find what you installed...

    rpm -qa | grep jdk
    

    ...and this to look up its information (in my case, using jdk1.8.0_121.

    rpm -qi jdk1.8.0_121
    

    You can look in the Relocations field to get an idea for where to start, or you can Change the -i parameter to -l to get a full listing of the files actually installed.

    You may be able to do as little as register this newly installed JDK with your IDE, and it will be smart enough to know where to find the source code. However, if that doesn't get the job done, you can find the src.zip file by grepping the full list of files installed like this:

    $ rpm -ql | grep 'src.zip'
    /usr/java/jdk1.8.0_121/src.zip
    

    Now just point your IDE at that zip file, and you're good.

    Open JDK

    The only trick to finding the source code for open JDK is to realize that they install their source code in a different package than the basic JDK.
    So using your package manager of choice, look for the available jdk packages. Look at the one you have installed and are working with, and find the pacakge with the same name and the -src extension.

    So if you've got java-1.8.0-openjdk, you should be able to find java-1.8.0-openjdk-src. Install that package, restart your IDE, and you should be able to view then source code.