How to Install the Java Development Kit

Dr. Andrew Broad
Computer Science
Java
How to Install the Java Development Kit


I distribute my Java programs as source-code files. To run them, you need to compile the source-code files to bytecode-files using a Java-compiler (javac), and then run the bytecode-files using a Java-interpreter (java). java and javac are command-line programs that you run in a Command Prompt.

Fortunately, both the compiler and the interpreter are freely downloadable as part of Sun Microsystems' Java Development Kit (JDK).

This page explains how to install the JDK on a PC running Windows XP, though it may be of some help to users on other platforms.

4th August 2007: added Steps 4 and 5.


Step 1: Is the JDK already installed on your computer?

(1.1) Open a "Command Prompt" window (Start menu > All Programs > Accessories > Command Prompt).

(1.2) In the Command Prompt, enter "javac" (without the quotation-marks).

If you get the following message...

'javac' is not recognized as an internal or external command, operable program or batch file.

...then either:
(a) JDK is not installed (see Step 2), or
(b) JDK is installed, but the "Path" environment-variable does not include the directory containing javac.exe (see Step 3). If you suspect that this may be the case, look for a directory on your hard drive called "C:\Program Files\Java\jdk1.6.0_02\" or similar, and make a note of this directory - you'll need it in Step 3.1.

(I use javac as the example, rather than java which may already be installed on your computer as part of the Java Runtime Environment (JRE).)


Step 2: Download and install the JDK

The latest release of the JDK (6u2 at the time I write this, i.e. version 6, update 2) is freely downloadable from Sun Microsystems.

(2.1) Go to http://java.sun.com/javase/downloads/.

(2.2) Click "Download" to the right of "JDK 6u2".

(2.3) Click the radio-button to the left of "Accept License Agreement" so that a little black dot appears in it.

(2.4) Below that, you will see a list of downloads for various platforms (Windows, Linux, Solaris). If you are a Windows-user, click "Windows Offline Installation, Multi-language".

(2.5) When you get to the "File Download" dialogue-box, click "Run" (or, if you prefer, save it and then run your saved copy). It may take several minutes to download.

(2.6) When the download is complete, Windows Installer will run. Follow the on-screen instructions until you get to "Custom Setup".

(2.7) When you get to Custom Setup, make a note of the directory where the JDK will be installed (e.g. copy & paste it into a text-editor). At the time I write this, this directory is the following:

C:\Program Files\Java\jdk1.6.0_02\
Obviously, this will change if you are installing a newer release than JDK 6u2.

(2.8) Custom Setup allows you to pick and choose which optional features you wish to install. Personally, I decided against installing the Public JRE, because I already have a perfectly good version of the JRE installed on my computer, so why replace it if there's the slightest risk I might regret doing so?

To deselect a feature, click the icon to the left of the name of the feature (e.g. "Public JRE"), click "Don't install this feature now", and a little yellow "1" will appear on the icon.

(2.9) Once you have completed Steps 2.8 and 2.9, click "Next", then wait while the Install Wizard installs the JDK.


Step 3: Add the JDK to your Path

Before typing "javac" at a Command Prompt will work (as per Step 1), you need to add the directory of which you made a note in Step 2.7 to your "Path" environment-variable.

The following instructions apply to users of Windows XP:

(3.1) Append "bin" to your note of the directory in which the JDK is installed (as per Step 2.7), e.g.

C:\Program Files\Java\jdk1.6.0_02\bin
This is the actual directory in which the programs java and javac (and many others) reside.

(3.2) Open the Control Panel (Start menu > Control Panel).

(3.3) In the Control Panel, double-click on System.

(3.4) In the System Properties window that just appeared, click the "Advanced" tab.

(3.5) Click the "Environment Variables" button.

(3.6) In "System variables", scroll down to "Path" and click on it (or simply click on any variable, then press 'P' so that "Path" is highlighted).

(3.7) When "Path" is highlighted (white ink on blue paper), click the "Edit" button below it.

(3.8) You are about to edit the value of the variable named "Path". If you edit it badly, you risk doing lasting damage to your system that you might not notice until a later date, when it may be very difficult to fix.

"Path" tells Command Prompt in which directories to look for the program whose name you type. Thus, when you type "javac", it will look in each directory listed in "Path", from left to right, until it finds the file javac.exe, which it then executes.

Note, then, that if you have two or more programs of the same name in different directories, then only the program in the leftmost directory listed in "Path" will be executed when you type its name in Command Prompt. This can be a problem when multiple versions of the same program exist on your hard drive!

So if you simply add ";C:\Program Files\Java\jdk1.6.0_02\bin" (without the quotation-marks) to the end of the value of Path, it may run the correct version of javac when you type it in Command Prompt, but if you type java, and JRE is already installed on your computer, it will be the old version of java that runs.

Therefore, you might want to add "C:\Program Files\Java\jdk1.6.0_02\bin;" (without the quotation-marks) to the beginning of "Path" instead of the end, so that it will be the first directory in which Command Prompt will look for java and javac.

So, after adding the JDK directory to the "Path" variable on a fresh computer, "Variable value" should look something like this:

C:\Program Files\Java\jdk1.6.0_02\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem

Note the syntax: a sequence of directories specified by their absolute paths, separated by semicolons (;). And in case you're wondering, %SystemRoot% refers to the "SystemRoot" environment-variable, i.e. "C:\WINDOWS".

It may help to copy the entire Variable value and paste it into your text-editor, and then copy & paste the edited value back into the "Variable value" slot. But beware of copying it from the text-editor with word-wrap on, as this may cause only the first line of the value to be pasted into "Variable value"!

(3.9) Once you are satisfied that you have edited "Variable value" correctly, click OK. If you are in any doubt, click Cancel and repeat Steps 3.7 and 3.8.

(3.10) Clicking "OK" in Environment Variables is what actually applies the change.

(3.11) Test that the installation of java and javac worked by typing them in a new Command Prompt (changes to environment-variables do not apply to instances of Command Prompt that were already open when those changes were applied), as per Step 1.


If you are using an older version of Windows to which Steps 3.2 to 3.10 don't apply, look for a file called AUTOEXEC.BAT in the root-directory of your boot-device (e.g. C:\), edit it using a text-editor (e.g. Notepad), and look for a line in that file beginning with "PATH=".
����������� Insert the appropriate directory (e.g. "C:\PROGRA~1\Java\jdk1.6.0_02\bin;" (without the quotation-marks) - note the use of the MS-DOS filename PROGRA~1 instead of "Program Files") immediately after "PATH=", or at whatever position in the line you feel is appropriate as per Step 3.8.

In UNIX, the equivalent of AUTOEXEC.BAT is .profile (which may refer to another file, typically .kshrc, in which PATH is defined), and the directories in PATH are separated by colons (:) rather than semicolons (;).


Step 4: Compile Java source-code files

After successful completion of the above three steps, it's a simple matter to compile and run a Java program such as those available for downloading from this site.

To compile a Java program:

(4.1) Save the source-code file(s) to a directory of your choosing. For example, suppose you save Program.java in "C:\Documents and Settings\Your Username\My Documents\Java".

(4.2) Navigate to that directory in a Command Prompt using one of the following methods:
(a) Type "cd C:\Documents and Settings\Your Username\My Documents\Java" (without the quotation-marks).
(b) Set a user-variable: follow Steps 3.2 to 3.5 above, click "New" under "User variables", and fill in the variable-name (e.g. "JAVA" - without the quotation-marks) and variable-value (e.g. "C:\Documents and Settings\Your Username\My Documents\Java" - without the quotation-marks); click "OK" three times, then open a new Command Prompt. You can then navigate easily to that directory by typing "cd %JAVA%" (without the quotation-marks) or whatever you called it.
(c) Use Drop to DOS: once you have installed it, you can quickly open any directory in a Command Prompt by right-clicking on that directory and selecting "Drop to DOS".

(4.3) Compile the source-code file(s) using either of the following methods in a Command Prompt:
(a) Compile each source-code file individually, e.g. type "javac Program.java" (without the quotation-marks).
(b) Compile all source-code files in the directory: type "javac *.java" (without the quotation-marks).
If you will be repeating the same compilations, you may wish to save the command(s) in a batch-file (e.g. "Program.bat") in the same directory as the source-files, and you can then simply double-click on the batch-file every time you want to compile.

Step 4.3 should generate a bytecode-file (e.g. Program.class) for each source-code file, if there are no compilation-errors. If a source-code file contains more than one class, then javac will generate a bytecode-file for each class.


Step 5: Add the directories containing your Java files to your CLASSPATH

After completing Step 4, you can run a bytecode-file by using the Java-interpreter in a Command Prompt, e.g. by typing "java Program" (without the quotation-marks). But first you have to navigate to the directory containing Program.class, as in Step 4.2.

To run a Java program that resides in a certain directory without having to navigate to that directory, you can add that directory to a system-variable called CLASSPATH.

To do this, follow Steps 3.2 to 3.10, but with "CLASSPATH" in place of "Path". Following the running example, you might append ";C:\Documents and Settings\Your Username\My Documents\Java" (without the quotation-marks) to the variable-value of CLASSPATH.

If CLASSPATH does not exist yet, click "New" under "System variables" first!


Email me
Hosted by www.Geocities.ws

1