view java source code
Is any way to view the actual code behind the default classes in Java (java.awt.*;
, javax.swin开发者_如何学编程g.*;
, 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).
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.
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
src.zip
usually comes with JDK.
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.
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.
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
精彩评论