Java Compiling and Running Java: JDK - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Java Compiling and Running Java: JDK - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, December 21, 2018

Java Compiling and Running Java: JDK

Java 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 on 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. 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, the commands are javac to compile and java to run your program (and, on Windows only, javaw to run a program without a console window). 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.

There is an optional setting called CLASSPATH, 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. Some other Java compilers 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, which is fast (C++), free, and available both for Windows and for Unix. It’s also easy to install and is included with the Mac OS X Developer Tools package. For Windows, 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 something like:

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/projects/jikes/ for Jikes information and downloads.

What I really like about Jikes is that it gives much better error messages than the JDK compiler does. It alerts 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.

Another alternative technology is Kaffe, a product that Transvirtual licenses but also makes available in open source form (at http://www.kaffe.org/) under the GNU Public License. Kaffe aims to be a complete JDK replacement, though it has moved rather slowly and is not quite a complete, up-to-date Java 2 clone as of this writing. Again, Kaffe is available for BSD Unix and for Linux in RPM format. Visit the Kaffe web site for the latest information on Kaffe.

Other freeware programs include Japhar, a Java runtime clone, available from http:// www.hungry.com/old-hungry/products/japhar/, and the IBM Jikes Runtime from the same site as Jikes.

If you really want to get away from the mainstream, consider investigating JNODE, the Java New Operating system Development Idea, at http://www.jnode.org/. JNODE is a complete operating system written in Java, a kind of proof of concept. At this point JNODE is probably not something you would use for your main desktop—I’ve booted it only under Virtual PC on Mac OS X—but it could become that someday.

Mac OS X

The JDK is pure command-line. At the other end of the spectrum in terms of keyboard-versus-visual, we have the Apple Macintosh. Books have been written about how great the Mac user interface is, and I won’t step into that debate. Mac OS X (Release 10.x of Mac OS) is a new technology base built upon a BSD Unix base. As such, it has a regular command line (the Terminal application, hidden away under /Applications/Utilities), as well as all the traditional Mac tools. It features a full Java implementation, including two GUI packages, Sun’s Swing and Apple’s own Cocoa. JDK 1.4.2 has been released for Mac OS 10.3 as of this writing; the latest version is always available from Software Update.

Mac OS X users can use the command-line JDK tools as above or Ant (see Recipe 1.7). Compiled classes can be packaged into “clickable applications” using the Jar Packager. Alternately, Mac fans can use one of the many full IDE tools.

No comments:

Post a Comment

Post Top Ad