Setting the PATH and CLASSPATH
The PATH is a variable your operating system uses to know where to find files to execute. For instance, if you type 'ls' at the command line in Unix, or 'dir' at the command line in Windows, what you are really saying is to execute the file (called 'ls' in Unix and 'dir' in Windows) that contains the program to list out a directory in your file system. The problem is, where should your operating system look for those files? The set of default places to look is always kept in a variable which the Operating System consults. So if you type 'javac MyFile.java', and your Operating System replies "Command not found" or something of that ilk, what is not being found is not your program MyFile.java, but the java compiler itself which is simply a file on your system called 'javac'. The reason it is not being found is because its location is not in the set of default places to look, which is kept in your PATH variable. To fix this, it is necessary to add the correct path to the PATH variable. In Windows the java tools, including the compiler (javac) and virtual machine (java) are normally kept in a location like
C:\Program Files\Java\jdk1.5.0_02\binalthough, as they say, your mileage may vary. The exact location can change from machine to machine and from installation to installation. To find out for sure, poke around a little :) -- it is a good idea to learn a little about where things are in any case. Alternatively, you could search for these files using the search mechanism in the Start Menu or in the file browser of a Linux GUI.
The CLASSPATH is a variable that Java itself uses to keep track of where various classes are located that it might need. You do not normally need to set any actual values in the CLASSPATH, only to make sure that it exists and that there is a single dot to denote the current directory.
Determining whether the PATH and CLASSPATH have been set:
Unix
Type these commands in a command window:
echo $PATH echo $CLASSPATHIf you get a blank command line in response to either of these, then that particular variable has no value (it has not yet been set). Alternatively, if a PATH is echoed which does not include the correct path for the java tools, then you know you need to add their correct location to the PATH variable. Finally the CLASSPATH should include a single dot.
Windows
Type these commands in a command window:
echo %PATH% echo %CLASSPATH%If you get the message "echo is on" for either of these, then that particular variable has no value (it has not yet been set). Again, if a path is echoed which does not include the correct path, then you know you need to modify the PATH variable.
Setting the PATH and initializing the CLASSPATH on Windows XP
(Java version 1.5)
Main Method -- setting Environment Variables in the Control Panel
First, make sure you know where the files java and javac are located. Write this complete path down.
The idea here is to navigate to START -> Control Panel -> System -> Advanced -> Environment Variables and then modify the PATH setting which is in the System variables window to include the path which you have written down. It is very important that you do not modify any other system variables while you do this, and that you not delete any paths already in the PATH variable. You want to add the new path to the ones already there. Notice that paths are seperated by a semicolon in Windows. Finally, you will want to enter a single dot '.' for the value of the CLASSPATH variable
- Click on START (lower left)
- Under "Settings" click on "Control Panel"
- Switch to "classic view" (upper left) -- not "category view"
- Click on the "System" icon.
- Click on the "Advanced" tab.
- Click on the "Environment Variables" button (bottom).
- Highlight "PATH" (in the top window) and click the "edit" button.
BE CAREFUL NOT TO EDIT OR MODIFY ANY OTHER SYSTEM VARIABLES!
- Edit the "PATH" so it begins C:\Program Files\Java\jdk1.5.0_02\bin; (or whatever path you have written down -- if you installed Java into a different directory, you need to put in the directory where it is installed).
MAKE SURE NOT TO CHANGE ANY OF THE PATHS ALREADY THERE!
- After you have finished editing the PATH, click "OK". You are done setting your PATH. Now you have to create a new variable, called your CLASSPATH.
- Click on the "NEW" button below the top window.
- Under 'variable name' type CLASSPATH
- Under 'variable value' type . (yes, type a single period).
- Click OK.
- EXIT the control panel.
- Restart your computer.
- Test your installation by opening up a DOS window and verifying that both the commands "java" and "javac" are recognized.
- Click on START (lower left)
- Click on run
- Where is says "open" type in "command" and click OK. This gets you to the DOS prompt.
- Type "cd c:\"
- Type "notepad autoexec.bat" -- this will bring up the notepad editor. In this editor type the following two lines:
set PATH=%PATH%;C:\Program Files\Java\jdk1.5.0_02\bin; set CLASSPATH=.
- Exit notepad.
The PATH and CLASSPATH variables in Unix are set in the initialization script for the shell you are using. For instance, if you are using bash, the settings will be contained in the '.bashrc' file or the '.bash_profile' file which are probably in your home directory. On my Mac, since I usually use tcsh which is the default, the file that initializes these variables is called '.tcshrc'. As above, you must edit this file and make sure that the PATH variable includes the correct path to your java tools (java and javac). Note that paths in Unix are delimited by a forward slash '/' instead of the backslash '\' which is used by Windows, and that the separator between paths in the PATH variable is a colon instead of a semicolon. Case matters in Unix (it is good practice in Windows). Set the CLASSPATH variable so that its first entry is a single dot (this says to look for classes by default in the current directory). Do not terminate either the PATH or CLASSPATH entry with the separator.
In general, the most common mistake in this process is mistyping. So be careful and double-check. If you still cannot get the java or javac command recognized, see ITS (Computer Services), or the tutors, or myself.