23

Is any way to view the actual code behind the default classes in Java (java.awt.*;, javax.swing.*;, etc) to see exactly what it is that is happening?

I don't mean just the documentation, or a list of methods, etc, but the source code itself in full detail (in other words, what could be used to create an exact copy of an entire method/class if copied and pasted into the code for a program in Java).

2
  • 3
    In an IDE such as IntelliJ you can browse the source of these packages just like anything else. Commented Oct 19, 2014 at 7:24
  • 2
    Extending @ChrisMartin, right click the keyword (e.g. String) in the editor -> "Go to" -> "Implementation(s)" ... as of idea 14.0.2 Commented May 4, 2015 at 14:05

6 Answers 6

26

The Java source code for all the API classes is shipped in the JDK installer in a file named src.zip. It's often just sitting in your install directory. Unzip it, and have a look.

If it's not there, you may have chosen not to install it; reinstall the JDK and watch for the "source code" option, making sure to include it.

Sign up to request clarification or add additional context in comments.

Comments

15

I frequently use http://docjar.com for this purpose.

Example: I want to see the source code for String. Search for the FQCN, java.lang.string (using the "Package/class Name" option). Click the result you want, then click the source link at the top of the page. Voila: http://www.docjar.com/html/api/java/lang/String.java.html

3 Comments

grepcode is another great online ressource for quick source code inspection, but as they print linenumbers, copy&paste of code from there into java source files is ... annoying ;)
@Andreas_D - just click the little "Raw" link on the top right of the screen.
@Andreas_D GrepCode is not working nowadays... Any idea? Have they redirected or something?
4

src.zip usually comes with JDK.

Comments

3

I also find http://www.codeatlas.com to be a great place to look for JVM based source code in general. For example, if you are ever looking for how java.awt.ActionEvent is implemented you can find it here:

http://www.codatlas.com/project/L_fXVCOhW4_lzXEd3R5DNQ__/master/src/share/classes/java/awt/event/ActionEvent.java?keyword=ActionEvent&line=59

What's nice about it is that it gives an IDE like browsing experience by adding cross-reference and syntax highlighting.

1 Comment

Links are broken.
1

For those who use linux distributions, like ubuntu, archlinux, etc. You can always get openjdk source code via corresponding package management system. This means you can choose a faster mirror to get the source code.

For ubuntu users:

$ sudo apt-get install openjdk-7-source

For Archlinux users:

$ sudo pacman -S openjdk7-src

The source will be located in the $JAVA_HOME, somewhere like /usr/lib/jvm/java-7-openjdk/src.zip.

Other distributions will have likewise package names, so not listed here.

Comments

0

1) Use Eclipse to attach the source code in C:\users\program files\jdkx.x
2) Use GrepCode if you are just browsing the code and to save memory used by heavy weight eclipse on your machine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.