O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Compiling and Running Java: JDK

Problem

You need to compile and run your Java program.

Solution

This is one of the few areas where your computer’s operating system impinges into Java’s portability, so let’s get it out of the way first.

JDK

Using the command-line Java Development Kit (JDK) may be the best way to keep up with the very latest improvements from Sun/JavaSoft. This is not the fastest compiler available by any means; the compiler is written in Java and interpreted at compile time, making it a sensible bootstrapping solution, but not necessarily optimal for speed of development. Nonetheless, using Sun’s JDK (or Java SDK), the commands are javac to compile and java to run your program. For example:

C:\javasrc>javac HelloWorld.java

C:\javasrc>java HelloWorld
Hello, World

C:\javasrc>

As you can see from the compiler’s (lack of) output, this compiler works on the Unix “no news is good news” philosophy: if a program was able to do what you asked it to, it shouldn’t bother nattering at you to say that it did so. Many people use this compiler or one of its clones. The javac and java commands are available with the JDK on both Windows and Unix, and under MacOS X if you have installed the bundled Developer Tools package.

There is an optional setting called CLASSPATH, discussed in Section 2.6, that controls where Java looks for classes. CLASSPATH, if set, is used by both javac and java. In older versions of Java you had to set your CLASSPATH to include “.” even to run a simple program from the current directory; this is no longer true on Sun’s current Java implementations. It may be true on some of the clones.

Command-line alternatives

Sun’s javac compiler is the official reference implementation. But it is itself written in Java, and hence must be interpreted at runtime. Recognizing the slowness of compilation as a significant hindrance to developers, Sun’s Java folk went back and rewrote the compiler from scratch, discarding some old baggage and using new language features. This new compiler (still named javac) was unveiled for early access in May 1999 and released later that year. It is about twice as fast as the original Java compiler -- a big improvement -- but still slower than some other compilers. Symantec’s Java compiler and Microsoft’s J++ (a Java-like language) are written in C/C++, so they are quite a bit faster than an interpreted Java compiler.

In order to speed up my compilations, I have used Jikes, a freeware compiler written in C++. Jikes is fast, free, and available both for MS-Windows and for Unix. It’s also easy to install. For MS-Windows (Win32), Linux, and other Unix systems, you can find binaries of the current version on IBM’s Jikes web site. If you are using OpenBSD, NetBSD, or FreeBSD, you should only need to run:

cd /usr/ports/lang/jikes; sudo make install

or just download the package file and use pkg_add to get it installed. Visit http://oss.software.ibm.com/developerworks/opensource/jikes/ for Jikes information and downloads.

A key benefit of Jikes is that it gives much better error messages than the JDK compilers do. It will alert you to slightly misspelled names, for example. Its messages are often a bit verbose, but you can use the +E option to make it print them in a shorter format. Jikes has many other command-line options, many that are the same as the JDK compiler’s, but some that go beyond them. See Jikes’s online documentation for details.

An older C++-based Java compiler, Guavac, is not considered finished. Indeed, its author has stopped maintaining it. Nonetheless, I was able to use Guavac 1.2 to compile many of the examples in this book (note that the Guavac version number of 1.2 is unrelated to the Sun JDK version number 1.2). See ftp://sunsite.org.uk/packages/guavac/for information on Guavac.

Another alternative technology is Kaffe, a product that Transvirtual (http://www.transvirtual.com) licenses but also makes available in open source form under the standard GNU Public License. Kaffe aims to be a complete JDK replacement, though it has moved rather slowly past the JDK 1.1 level and is, as of this writing, still not quite a complete Java 2 clone. Again, on OpenBSD there is a port, and on Linux there are RPMs available. Visit Transvirtual’s web site for the latest information on Kaffe.

One last freeware package is Japhar, a Java runtime clone, available from http://www.japhar.org.

MacOS

The JDK is purely command-line-based. At the other end of the spectrum in terms of keyboard-versus-visual, we have the Apple Macintosh. Whole books have been written about how great the Mac is, and I won’t step into that debate. I will, however, comment on how lamentable it is that Apple let its Java implementation lag behind current standards. Users of MacOS 8 and 8.5 have put up with Java 1.8 for several years. MacOS X (Release 10 of MacOS) is a new technology base built upon a BSD Unix base. As such, it has a regular command line as well as all the traditional Mac tools. And it features a full Java 2 implementation, including Swing.

For MacOS 8, if you’ve followed Apple’s directions for installing the MacOS Runtime for Java (MRJ), you can compile by dragging a file to, or double-clicking on, the “javac” icon (I’ve made aliases for this icon and friends on my desktop). Once the dialog shown in Figure 1-1 appears, you can click on “Do Javac” (or just press Enter on the keyboard), first changing any options if you want.

MacOS 8 Javac window

Figure 1-1. MacOS 8 Javac window

You will then see the Java console window stating that it ran javac (as shown in Figure 1-2). This javac is a Mac port of the JDK version, so it also runs on “no news is good news.” As this is a Mac, you’ll see the resulting class file appear in your destination folder as soon as it’s been created (which happens only if there are no compilation errors).

MacOS 8 compilation completed (MRJ)

Figure 1-2.  MacOS 8 compilation completed (MRJ)

You now have a class file, and you want to run it. That’s where the JBindery program comes in. JBindery can do two things: run a Java class file directly or make it into a “clickable” runnable program. We’ll start it by dragging the class file onto the Jbindery icon; the program starts as shown in Figure 1-3.

MacOS 8 JBindery window

Figure 1-3. MacOS 8 JBindery window

As we are running a simple command-line program rather than a windowed application, after we click on “Run,” the JBindery screen is replaced by a Java Console showing the command output, as in Figure 1-4.

MacOS 8 Java Console showing program output

Figure 1-4. MacOS 8 Java Console showing program output

Macintosh users who can run MacOS X have more choice. Since MacOS X is a hybrid of Unix and MacOS X, they can use the command-line JDK tools directly and then build the application using the "Build Application” tool. Figure 1-5 shows this running with the Application Look and Feel Switcher from Section 13.13. This builds a folder or directory containing all the pieces needed to make a clickable application. Or, they can use a full IDE, as discussed in Section 1.4.

MacOS X application builder

Figure 1-5. MacOS X application builder

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required