The Java Developer's Resource, Chapter 2: Installing Java

In this chapter

Installing Java In this chapter, you download the software you need to run Java from the Internet, install it, and run your first applet.

Getting Java Software for your Platform

The basic Java environment consists of a web browser that can play Java applets, a Java compiler to turn to Java source code into byte code, and a Java interpreter to run Java programs. You'll also need a text editor like emacs, TextPad, or BBEdit. Other tools like a debugger, a visual development environment, and a class browser are nice but aren't absolutely necessary.

It isn't necessary to get all pieces from the same source. For instance, your web browser will probably be the same copy of Internet Explorer or Netscape you already use. The rest will probably be provided by Sun's Java Developer Kit (JDK). Sun publishes versions for all the platforms it supports (Windows, Solaris, X86 Linux) and third parties have ported it to other platforms.

The JDK does not include a web browser. Instead it includes an applet viewer that lets you view and test your applets. The JDK also includes the javac compiler, the java interpreter, the javaprof profiler, the javah header file generator (for integrating C into your Java code), the Java debugger and limited documentation. However most of the documentation for the JDK is on Sun's web site.

Sun provides Java virtual machines for Solaris, X86 Linux, and Windows 95, 98, and NT. These are available in a number of configurations and versions. For purposes of this book you'll need the Java 2 Software Development Kit, version 1.2 or later. Most people just call this the JDK 1.2. The most recent version of the JDK is 1.3, and this will work as well. However, JDK 1.3 isn't available for all platforms yet, and the differences between JDK 1.2 and JDK 1.3 aren't very important for the purposes of this book.

Many different companies have ported various versions of the JDK to different platforms including:

Windows 95, 98, NT, 2000
MacOS 7.6.1 and later
Linux X86
HP/UX
AIX
IRIX
Solaris
OS/2

Integrated Development Environments

Java development tools are still fairly primitive compared to what's available for C++. Therefore this book does not assume you'll be using an integrated development environment. So far I've found all the IDEs I've tried to be actively destructive at worst and almost as usable as the raw JDK at best. This includes but is not limited to Metrowerks Code Warrior, Borland JBuilder, WinGate Visual Cafe, Sun's Java Workshop, and Microsoft Visual J++. IDEs are getting better, but they still tend to cause more problems than they solve. I recommend you do not use an IDE.

If you absolutely must use an IDE, I'll issue a particular warning about Visual J++. It's buggy, out-of-date, non-standard, and causes as many problems as all other IDEs combined. Do not use Visual J++ under any circumstances!

Installing Java

Once you've downloaded the JDK, you'll need to install it. This is quite easy on the Mac, not too difficult on Unix, and quite a chore on Windows, especially if you're installing over an older version of the JDK. Here are the detailed instructions.

Windows Installation Instructions

First deinstall any previous versions of the JDK you may have installed, especially if you want to put the new JDK in a different directory. You may also have to use regedit to delete any keys previous installations have left behind. Be careful, though. regedit is a dangerous tool.

The Win32 X86 releases are self extracting archives. You will need about sixty megabytes of free disk space to install the JDK. Execute the file by double-clicking on its icon in the File Manager or by selecting Run... from the Program Manager's File menu and typing the path to the file. This will unpack the archive including all necessary directories and sub-directories. The full path is unimportant, but for simplicity's sake this book assumes you install it in C:\jdk. If you unpacked it somewhere else, just replace C:\jdk by the full path to the directory where you put it in what follows.

You will need to add C:\jdk\bin directory to your PATH environment variable. For example,

C:>set PATH="C:\jdk\bin;$PATH"

You can do it more permanently by putting this command in your autoexec.bat file. In Windows NT, you should set it from the Environment tab of the System Control Panel as shown in Figure 2-1 instead.

Figure 2-1: Setting the PATH environment variable under Windows NT

To make sure your Java environment is correctly configured, bring up a DOS window and type "javac nofile.java". For example,

C:\>javac nofile.java

If your computer responds with "error: Can't read: nofile.java ", you're ready to begin. If, on the other hand, it responds

The name specified is not recognized as an
internal or external command, operable program or batch file.

or something similar, then the Java environment has not been properly installed or your PATH is not set correctly. You'll need to fix it before continuing.

Linux/Unix Installation Instructions

If you have an account on a shared system at a university or an Internet Service Provider, there's a good chance Java is already installed. Ask your local support staff how to access it. Otherwise follow these instructions.

The Unix release is a gzipped tar file. You will need about sixty megabytes of free disk space to uncompress and untar the JDK. Double that would be very helpful. You do this with these commands:

% gunzip jdk1_2_2-linux-i386.tar.gz
% tar xvf jdk1_2_2-linux-i386.tar

The exact filename may be a little different if you're retrieving the release for a different platform such as Irix or if the version is different.

You can untar it in your home directory, or, if you have root privileges, in some convenient place like /usr/local/java where all users can have access to the files. However root privileges are not necessary to install or run Java.

Untarring the file creates all necessary directories and sub-directories. The exact path is unimportant, but for simplicity's sake this book assumes it's installed it in /usr/local/java. If a sysop already installed it, this is probably where it lives. (Under Solaris it's also possible the sysop put it into /opt.) If this is the case the files live in /usr/local/java/jdk1.2.2. If you're unpacking a different version, then you may find it installed in a slightly different directory such as /usr/local/java/jdk1.3. This allows multiple versions of the JDK to coexist harmoniously on the same system. If you unpacked it somewhere other than /usr/local/java, just replace /usr/local/java by the full path to the java directory in what follows. If you installed it in your home directory, you can use ~/java instead of a full path.

You now need to add /usr/local/java/jdk1.2.2/bin directory to your PATH environment variable. You use one of the following commands depending on your shell.

csh, tcsh:
% set PATH=($PATH /usr/local/java/jdk1.2.2/bin)
sh:
% PATH=($PATH /usr/local/java/bin); export $PATH

You should also add these lines to the end of your .profile or .cshrc files so you won't have to repeat them every time you login.

To verify that your Java environment is correctly configured, type "javac nofile.java" at a shell prompt like this

% javac nofile.java

If your computer responds with

error: Can't read: nofile.java

you're ready to begin. If, on the other hand, it responds

javac: Command not found

or something similar, then you the Java environment is not properly installed or your PATH is incorrect. You'll need to fix it before continuing.

Macintosh Installation Instructions

The file you get will be a self-mounting image called something like MRJ_2.2.smi.bin. If you use Fetch, Anarchie, or a web browser to download it will be automatically converted into the self-mounting image called MRJ_2.2.smi.bin. Double-click it to mount it and then run the installer from the image it mounted. The installer will prompt you for a folder to install it in on your computer. Put it wherever is convenient.

It will probably be helpful to make aliases of the Applet Viewer, the Java Compiler and the Java Runner and put them on your desktop for ease of dragging and dropping later, especially if you have a large monitor.

Running Your First Applet

Now that you've installed the JDK or its equivalent for your platform of choice, you're ready to run your first Java program, the TicTacToe applet, a standard demo included with the JDK and shown in Figure 2-2. Once again, I'll provide separate instructions for Windows, the Mac, and Unix.

Windows Instructions

Start the Applet Viewer by doing the following:

  1. Open a DOS Window and cd to one of the directories in C:\jdk\demo; for example,

    C:\>cd C:\jdk\demo\applets\TicTacToe

  2. Type "appletviewer example1.html" and hit return like this:

    C:\>appletviewer example1.html

  3. Play Tic-Tac-Toe! It is allegedly possible to win.

Unix/Linux Instructions

Start the Applet Viewer by doing the following:

  1. From the command line, cd to one of the directories in /usr/local/java/jdk1.2.2/demo, for example

    % cd /usr/local/java/jdk1.2.2/demo/applets/TicTacToe

  2. Run the appletviewer on the file named example1.html:

    % appletviewer example1.html

  3. Play Tic-Tac-Toe! It is allegedly possible to win.

Macintosh Instructions

  1. Start the Applet Viewer by double-clicking it.
  2. Select Open... from the File menu and navigate into the java folder, then the Sample Applets folder, then the TicTacToe folder.
  3. Select the file example1.html and click on the Open button. Alternately you can drag and drop this file onto the Applet Viewer.
  4. Play Tic-Tac-Toe! The algorithm was deliberately broken so it is possible to win.

Figure 2-2: The Tic-Tac-Toe applet by Arthur van Hoff

Summary

In this chapter you installed the Java Development Kit and ran your first Java applet. The things you learned in this chapter included:

Before you continue you should have Sun's Java Development Kit 1.1 or later installed and you should have run the TicTacToe applet in the Applet Viewer.

Coming Up

In the next chapter you will write your first Java program, Hello World.

Questions and Answers

Exercises

Further Reading


Previous | Next | Top | Cafe con Leche

Copyright 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified May 30, 2000