ICS 45J Fall 2012
Setting Up Java and Eclipse On Your Computer

A word of warning

One of the advantages of programming in Java is that there are easy--and cheap--ways to set up a working Java environment on your own computer. On the other hand, working in the lab has some genuine benefits, particularly for those of you who are new to ICS. In the lab, your development environment will be the same as the production environment, so you won't have to worry about whether what you wrote "at home" will work "in the lab." It's also a great place to work with your partner, and to talk over assignments with other ICS 45J students. It also tends to be less distracting than working elsewhere.

Nevertheless, we're aware that many of you will choose to do some percentage of your work on your own machines, so, as a courtesy, we provide below enough information to have your Windows-based or Macintosh computer support the Eclipse development environment and Oracle's release of Java, using freely available software. You are free to use any development environment you like, but your resulting programs must work in the "production environment" for this course; that is, using Oracle's version of Java running under Eclipse. So, if you are completing the assignments on your own computer, we strongly recommend you test them on the lab computers before turning them in.


Windows and Mac OS X Lion and Mountain Lion installations only

This document only describes how to set up Sun's Java 7 and Eclipse Classic (Indigo)--the environment for this class--on a computer running Windows or Mac OS X 10.7 (Lion) or 10.8 (Mountain Lion). To install Java under these operating systems, see the applicable section below.

(If you want to run Java under a version of Mac OS X prior to Lion, there is a version from the OpenJDK group, but it requires a fair bit of Unix and can be tricky to install; unless you are pretty familiar with Unix programming and package installation on Macs, we suggest update your O/S and install Sun's Java rather than the Open OS Java package.)

If you use Linux or Solaris, JDK 7 Update 7 can be downloaded from Java SE Downloads, the same way it can for Windows; we do not discuss the details of installation here, but there is ample documentation available on the web.


Setting up Java on Windows

Removing Eclipse and previous versions of Java

First, it's best if you delete Eclipse and uninstall all earlier versions of Java you have installed on your machine. (Use the "Add/Remove Programs" utility in the Microsoft Windows Control Panel to uninstall Java; delete the eclipse folder to delete Eclipse.) Leaving old versions causes setup of Java 7 to be more complicated and problematic. Further, an old version of Eclipse will not recognize the new version of Java without some rather messy settings changes, whereas installing Eclipse after installing the new Java will cause Eclipse to find and use Java 7 without any fuss. If you absolutely must keep older versions of Java on your machine, the instructions below may not be sufficient to have the new version of Java work correctly—you'll need to seek additional assistance.

Getting started

What you'll need to get started is the latest production (non-beta) version of the Java SE 7 JDK (JDK 7) from Java SE Downloads, since this is the version we use in the lab. Just click on the JDK Download button in the Java SE 7u6 section of the screen, accept the license agreement, choose the version of Java that matches your operating system and save the downloaded exe file. For those using Windows 7 and earlier, related OS, such as Windows Vista, 2000, and XP, choose the download for Windows x86 systems. The x86 version will work even if you have a 64-bit (Itanium-based) machine. If you would rather install the 64-bit (x64) version of Java 7 on your 64-bit machine, go ahead and download the 64-bit vesion's exe.

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.)

Installation: the easy part

The JDK includes an installation program, which places all of the files into the appropriate places and sets up some desktop shortcuts for you: Double-click on the exe file you downloaded; if asked, say "yes" to allowing the program to modify your computer, and then follow the instructions on the installation screens (it's typically best to just accept the default settings). On 32-bit machines, the Java folder will be put inside the Program Files folder on the C: drive; that is, its path will be C:\Program Files\Java. However, if you are installing the 64-bit version of Java, or the 32-bit version on a 64-bit machine, the path may well be different. Do make a note of the exact path used. You will need it for the steps below.

Once Java is installed, the installation program may bring up a screen for you to install JavaFX. We do not use Java FX in this course, so feel free to cancel that (part of the) installation.

When installation completes, you'll be taken to a web page to register your installation; registration is optional.

With installation complete, you still won't be able to readily execute javac or java until you set the PATH and CLASSPATH properly.

Setting the PATH and CLASSPATH: the (not all that) hard part:

You can execute programs from a command prompt by simply typing their names, such as javac or java. And Eclipse iissues these commands behind hte scenes when you run a program inside Eclipse. But there's a catch: Comand prompt and Eclipse need to know where to find these programs. That is the job of the PATH environment variable. Whenever a command to execute a program is given (either by you or some running process on the computer), Windows looks for it in the list of folders in the PATH. Each folder in the PATH list is separated from the next by a semicolon; forgetting or misplacing semicolons or backslashes is a major cause of the PATH not finding javac or java.

Similarly, whenever the Java compiler or Java virtual machine needs 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 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. Forgetting or misplacing semicolons or backslashes is a major cause of the CLASSPATH not finding clases your program needs.

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 get to these environment variables is slightly different on different versions of Windows.

On Windows 7 and Vista, click the start orb in the lower-left corner of the screen, move your mouse over Computer, right-click, and select Properties from the pop-up menu. Click the Advanced System Settings link, then click the Environment Variables... button.

On Windows XP and 2000, right-click the My Computer icon on your desktop and select Properties. Select the Advanced tab. Click the Environment Variables... button.

Once you've brought up the environment variables, find the PATH variable listed under "System variables". If Java was never previously installed on this machine, add to the end of the PATH the path to the Java folder that the installation program created. If you are using a 32-bit Java, the text to add is

 ;C:Program Files\Java\jdk1.7.0_06\bin 

Do note the semicolon (;) that starts the text: it must be present.

If you installed Java on a 64-bit machine, you might need to change the \Program Files\Java part of the text above to match the path to the Java folder on your machine; for example, if Java was installed in the \Program Files (x86) folder, the path text would be
 ;C:\Program Files (x86)\Java\jdk1.7.0_06\bin 

If Java was previously installed, somewhere in the PATH there is likely to be text very similar to the above, differing only in the text between jdk(and \bin). Change that part of the path to match the text above.

To check that the PATH is properly set, call up a new Command Prompt window and enter javac -version.

If you see javac 1.7.0_06 or similar text, your path is properly set. If you instead get an error message that javac cannot be found, the PATH is not right; you'll need to go back and fix it.

Now 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 the name, and, for the value, the same text you used for the PATH variable, except have it end in lib instead of bin. For example, the CLASSPATH text for a standard 32-bit Java installation would be

 .;C:\Program Files\Java\jdk1.7.0_06\lib 

If the CLASSPATH does exist, which it likely will if Java was previously installed on this computer, change the CLASSPATH value text to match the text you added to your PATH, replacing bin with lib.

If you're using javac and java from the command line, you will need to bring up a new Command Prompt window, in order for these changes to take effect.

That's it!

At this point, your Java installation is ready to go; you're now ready to proceed to installing Eclipse.


Setting up Eclipse on Windows

Downloading Eclipse

There are several different distirbutions of Eclipse available for download at eclipse.org, each geared toward users with differentneeds, such as Java programming and web development. For our work in this course, we expect you to be using Eclipse Classic 4.2. Start by downloading it: click on the big Download Eclipse button, then find Eclipse Classic 4.2. If you are using Windows 7 or an earlier, related OS such as Vista, 2000, or XP, click the Windows 32 Bit button; if you have a 64-bit machine, you can download the 64-bit version of Classic instead. Then click on a download site and save the downloaded ZIP file to your computer.

Installing Eclipse

Eclipse is distributed as files in a Zip archive and, unlike most software you install on Windows, has no installation program. Instead, you do a few things on your own.

From here, your next step depends on which version of Windows you're running.

On Windows 7. Double-click on eclipse.exe to start Eclipse. When asked for a workspace, use the default for now. Once the program starts, you'll see the Eclipse icon show up on your taskbar. Right-click that icon and select Pin this program to taskbar, so that you'll be able to run Eclipse any time you'd like.

On Windows 2000, XP, and Vista. Right-click on the eclipse.exe file you found, select Create Shortcut to create a shortcut to allow you to run Eclipse. Drag this shortcut to your desktop or Quick Launch bar. You can then use this shortcut to open Eclipse any time you'd like.

That's it! Eclipse is installed and ready to go.


Setting up Java on Mac OS X Lion or Mountain Lion

Getting Started

What you'll need to get started is the latest production (non-beta) MAC version of the Java SE 7 JDK (JDK 7) from Java SE Downloads, since this is the version we use in the lab. Just click on the JDK Download button in the Java SE 7u6 section of the screen, accept the license agreement, and click on the Mac OS download from the Java SE Development Kit 7u6 download list.

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.)

Installation: the easy part

The JDK includes an installation program, which places all of the files into the appropriate places and sets up some desktop shortcuts for you. The Mac download is a disk image (.dmg) file. Double click on it and a pseudo-disk will appear on your disktop and a JDK 7 Update 06 window will appear; click on the .pkg (package) icon in the window to begin installation. As installation progresses, follow the instructions on the screens. Close the update window when installation completes. (You can also dismount the disk and put the .dmg file in the trash.

Checking if you have the latest version installed and selected as default

The Mac will not necessarily use the new Java if a previous version of Java was installed. To see what version of Java is being used, open a new Terminal window (by double-clicking on the Terminal application—it's usually found in the Utilities folder inside the Applications folder), then type the following command:

 java -version 

If you have the latest version of Java installed, you should see lines of text, with the first line being

 java version "1.7.0_06" 

If you see that the version number starts with "1.7.0," you're in business! Your machine is ready to go.

If you see a version number that starts with something other than "1.7" (like "1.6" or "1.5"), you'll need to continue following these instructions.

Checking what version of Java is the default on your machine

Mac OS X is perfectly happy to let different versions of Java coexist on your machine; installing a newer version of Java doesn't remove any earlier versions and doesn't even make the newer version the default Java on your machine. So, the next step is to make Java 1.7 the default version of Java.

Launch Applications, then select Utilities, then Java Preferences. Click on the General tab, if it's not already selected. In the Java Application Runtime Settings box, see if Java SE 7 (64 bit) is in the list; if so, drag it to the top of the list, making sure that it is the first item. Close the dialog box.

If Java SE 7 (64 bit) is not in the list at all, you'll need to download and install the latest version of Java for your OS, as discussed above, then make it the default version (if it is not already) as discussed in this section.


Setting up Eclipse on Mac OS X

Determining which version of Eclipse is right for you

There are two separate downloads of Eclipse Classic 4.2 for Mac OS X on the main download page, Mac OS X 32 Bit and Mac OX X 64 Bit. You want the latter version: if you are running Lion or Mountain Lion, your Mac must be a 64-bit machine. (The 32-bit version of Eclipse is for Macs on the order of ten or more years old. One could not run Java on such a machine, but Eclipse supports many languages, some of which have 32-bit versions.)

Installing Eclipse on Mac OS X

To install Eclipse Classic 4.2: