Finding More Java Source Code

Problem

You want even more Java code examples to look at.

Solution

Use The Source, Luke.

Discussion

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 (http://java.oreilly.com). 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 src.zip 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, java.io.Reader 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 http://www.gamelan.com still worked the last I checked, but the site has been (naturally) commercialized, and is now part of http://www.developer.com.

  • 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 http://www.gjt.org.

  • 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 http://www.collab.net.

  • SourceForge, also not specific to Java, offers free public hosting of open-sourced projects. See http://www.sourceforge.com.

  • Finally, the author of this book maintains a small Java site at http://www.darwinsys.com/java/, 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.