Finding More Java Source Code


You want even more Java code examples to look at.


Use The Source, Luke.


Java source code is everywhere. As mentioned in the Preface, all the code examples from this book can be downloaded from the O’Reilly site ( What I didn’t tell you, but what you might have realized by extension, is that the source examples from all the O’Reilly Java books are available there too: the Java Examples in a Nutshell book; the Java Swing book; all of them.

Another valuable resource is the source code for the Java API. You may not have realized it, but the source code for all the public parts of the Java API are included with each release of the Java Development Kit. Want to know how java.util.ArrayList actually works? You have the source code. Got a problem making a JTable behave? Sun’s JDK includes the source for all the public classes! Look for a file called or src.jar ; some versions unzip this and some do not.

If that’s not enough, you can get the source for all of the JDK for free over the Internet just by committing to the Sun Java Community Source License and downloading a large file. This includes the source for the public and non-public parts of the API, as well as the compiler (written in Java) and a large body of code written in “native” code ( C/C++): the runtime itself and the interfaces to the native library. For example, has a method called read( ) , which reads bytes of data from a file or network connection. This is written in C because it actually calls the read( ) system call for Unix, MS-Windows, MacOS, Palm, BeOS, or whatever. The JDK source kit includes the source of all this stuff.

And ever since the early days of Java, there have been a number of web sites set up to distribute free-software or open source Java, just as with most other modern “evangelized” languages such as Perl, Python, Tk/Tcl, and others. (In fact, if you need native code to deal with some oddball filesystem mechanism in a portable way, beyond the material in Chapter 10 of this book, the source code for the above-mentioned languages’ runtime systems might be a good place to look.)

I’d like to mention several web sites of lasting value:

  • Gamelan has been around almost forever (in Java time). The URL still worked the last I checked, but the site has been (naturally) commercialized, and is now part of

  • The Giant Java Tree is more recent, and is limited to code that is covered by the GNU Public License. There is a great deal of source code stored there, all of which can be freely downloaded. See

  • The CollabNet open source marketplace is not specific to Java, but offers a meeting place for people who want open source code written and those willing to fund its development. See

  • SourceForge, also not specific to Java, offers free public hosting of open-sourced projects. See

  • Finally, the author of this book maintains a small Java site at, which may be of value. This is the prime spot to obtain the JabaDex program, a longer (6,000-line) application that demonstrates some of the principles and practices discussed in the book. There is also a listing of Java resources and material related to this book.

As with all free software, please be sure that you understand the ramifications of the various licensing schemes. Code covered by the GPL, for example, automatically transfers the GPL to any code that uses even a small part of it. And even once looking at Sun’s Java implementation details (the licensed download mentioned previously) may prevent you from ever working on a “clean-room” reimplementation of Java, the free-software Kaffe, or any commercial implementation. Consult a lawyer. Your mileage may vary. Despite these caveats, the source code is an invaluable resource to the person who wants to learn more Java.

Get Java Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.