ICS 184 / EECS 116 Summer 2006
Setting Up Java On Your Computer


A word of warning

One of the advantages of programming in Java is that it's possible to set up your own computer as a Java development environment easily and cheaply. On the other hand, working in the lab has some genuine benefits, particularly for those of you who are fairly new to programming and/or fairly new to UCI. In the lab, you'll find that help is more readily available when you get stuck, that your machine setup will generally be stable and designed well for the task of working on your assignments, and that you're free from the distractions of your home.

Nevertheless, I'm aware that many of you, either out of want or need, will choose to do some percentage of your work at home. What follows is enough information to turn your Windows-based or Macintosh computer into a Java development environment at little or no cost.


What's in this document?

Before you get started, be aware that this document only describes how to set up Java on a computer running Windows or Mac OS X.

If you use Linux, J2SE 5.0 can be downloaded from java.sun.com, the same way it can for Windows. You're on your own as far as installing it goes, but I assume you can handle that, or you wouldn't be Linux users. :)

The document is broken into separate sections detailing how to install Java on Windows and on Mac OS X; these sections follow.


Setting up Java on Windows

Getting started

First and foremost, you need a Java compiler and Java virtual machine, both of which must support Java 5.0. A great option is to download the Java 2 Standard Edition (J2SE) JDK from java.sun.com. This is available absolutely free! The latest version is 5.0 Update 7. Be sure you download the JDK and not the JRE! The JDK includes a Java compiler (javac), a Java virtual machine (java), and all of the standard Java library components -- in other words, everything you need to write Java programs in a wide variety of problem domains, and more than enough firepower for the assignments we'll be giving you. (The JRE is the Java Runtime Environment, which includes the necessary code to allow you to run Java programs, but not to write and compile them! The JRE will not be sufficient for our work in this course.)

Secondly, you'll need a text editor, which you'll use to write and edit your program code. There are many inexpensive and/or free options available. In the lab, we provide TextPad, which you can download from www.textpad.com. TextPad requires a modest registration fee, which you should pay if you plan to use it on a continuous basis. You may be able to find free text editors, particularly those written in Java, at shareware/freeware download sites such as www.download.com.

Installation: the easy part

Presumably, your text editor will include an installation program to set it up and make it completely ready for your use.

The JDK also includes an installation program, which places all of the files into the appropriate places and sets up some desktop shortcuts for you. However, after running the installation program, you still won't be able to execute javac or java, without making a couple of modifications "under the hood" of your system.

When asked what folder you should install the JDK into, the default will be:

C:\Program Files\Java\jdk1.5.0

I suggest changing it to the following folder instead:

C:\jdk5.0

The reason for this suggestion is that the next steps in the installation process will be simpler if the folder you choose has a shorter name that, importantly, has no spaces in it.

The (not all that) hard part: PATH and CLASSPATH

You can execute programs from a Command Prompt (or, as Windows 95/98 call it, an "MS-DOS prompt") by simply typing their names, such as javac or java. But there's a catch: Windows has to know where it can find these programs. This is the job of the PATH environment variable. PATH is a list of folders, separated by semicolons, in which Windows will look for a program whenever you try to execute one.

Similarly, whenever the Java compiler or Java virtual machine need to be able to use a Java class (such as when you import a class from the Java library), the compiler or virtual machine needs to be able to know where to find it. This is the job of the CLASSPATH environment variable. CLASSPATH is a list of folders, separated by semicolons, in which the Java compiler or virtual machine will look for a Java class whenever it's trying to find one.

So, to make the JDK work, you have to modify the PATH and CLASSPATH environment variables on your system, so that Windows will be able to find javac and java, and so that javac and java will be able to find all of the necessary Java classes.

How you modify these variables depends on which version of Windows you're using. In either case, I'll assume that you've installed the JDK into the folder I suggested during installation, which is C:\jdk5.0. If you installed it somewhere else, you'll need to use your installation directory instead of the one above.

On Windows 2000 and XP. Right-click the My Computer icon on your desktop and select Properties. Select the Advanced tab. Click the Environment Variables... button. Under "System variables," find the PATH variable and add this to the end of it:

;C:\jdk5.0\bin

Then find the CLASSPATH variable. If it doesn't exist (and it probably won't, if you've never used your system to develop Java code), click the New... button, specifying CLASSPATH as its name, and the following value:

.;C:\jdk5.0\lib

You may need to restart your system, or at least start up a new Command Prompt window, in order for these changes to take effect.

On Windows 95, 98, and probably ME. (I say "probably" because I don't have a version of ME handy to test this with.) On these versions of Windows, you can set the PATH and CLASSPATH by modifying the file C:\AUTOEXEC.BAT. Add the following two lines to the end of this file:

set PATH=%PATH%;C:\jdk5.0\bin
set CLASSPATH=%CLASSPATH%;.;C:\jdk5.0\lib

You'll probably need to restart your system for these changes to take effect.

That's it!

At this point, you should be good to go. Enjoy!


Setting up Java on Mac OS X

Getting started

First, your Mac has to be running MacOS X 10.4.2 (Tiger) or later. If you're running an earlier version, it's time to upgrade; the current Java won't run on your machine. (On Mac OS X, you can find your version number by selecting About This Mac under the apple menu.) If you're running MacOS 9, it's really time to upgrade — probably to a new machine.

Finding help if you get stuck during this process

Because I'm not a Mac user, I unfortunately won't be able to help if you get stuck while following this process. If you're having trouble, I'd encourage you to seek help from knowledgeable friends and/or other students, perhaps by posting a message to the course newsgroup.

Downloading and installing Java

Download the current version of J2SE 5.0 from Apple. Double-click the installer package and follow the instructions to complete the installation.

Making J2SE 5.0 your default Java version

MacOS X is perfectly happy to let different versions of Java coexist on your machine; installing J2SE 5.0 doesn't remove any earlier versions and doesn't even make J2SE 5.0 the default Java on your machine.

To make J2SE 5.0 the default, follow these steps:

Testing your Java installation

To test your installation, first open a new Terminal window (by command-N or choosing New Shell on the File menu); then type this command:

java -version

You should see something like the following

java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-83)
Java HotSpot(TM) Client VM (build 1.5.0_05-48, mixed mode, sharing)

(If you see something without "1.5" and with "1.4" or some other version, then you'll need to get further assistance.) One possible cause is that one or both of the two "alias" lines was copied with a carriage-return in the middle; each of the two lines needs to be unbroken. A second possible cause is that you're using a different shell program than the current default. To find out, choose Preferences from the Terminal menu in the Terminal application. If you see /bin/bash in the first box, you're using the current default and you'll need to find an expert. But if you see something else there, click the "Execute this command" button and type /bin/bash in the box. Then quit Terminal, start it again, and check your Java version.)

To test your Java version, you can also try to run this simple program:

import java.util.*;

public class Java5Test
{
    public static void main(String[] args)
    {
        // This statement would make no sense to a pre-5.0 compiler
        ArrayList<String> a = new ArrayList<String>();
        a.add("Alex");
        a.add("is");
        a.add("happy");

        // This loop construct is also gibberish to a pre-5.0 compiler
        for (String s : a)
        {
            System.out.println(s);
        }
    }
}

If it runs successfully, you know you have J2SE 5.0 correctly installed. If you see error messages, either (a) you copied or entered the program incorrectly or (b) your installation of J2SE 5.0 was not successful; in either case, you may need to seek additional assistance.

Running Java programs

The procedure for running Java programs is described in detail elsewhere, but here is a synopsis:

What else you'll need to know

If you're used to the Mac environment and not used to Unix or other command-line processing systems, you'll want to learn at least a little Unix so you can find your way around. Those details are available elsewhere.