The CLASSPATH and Environment Variables
This tutorial/discussion is designed to help in understand and set your System Environment Variables. The objective is to provide the information needed to properly set up a Java environment on your system. You may find this information to be useful for other operations as well. :0)
Before we get started, you can call your resources explicitly without the need to set some Environment Variables. For instance you can start a Java program as follows:
C:\jdk1.3.1\bin\java -jar C:\windows\desktop\myfiles\SimpleColorConverter.jar
This is, of course, quite long winded and error prone. If you have to type it in over and over it will be a pain. Programmers are lazy. We only want to type the minimum amount of keystrokes so Environment Variables are used to save time.
Introduction
---------------
Environment Variables are "pointers" to programs, scripts, or other resources. Most systems have a limited number of Environment Variables pre-set and you can add your own. These variables are frequently associated with a shell such as Bash, Dos, Awk, Tcsh, etc. Systems will often make use of these variables as well. When you open a shell these variables are loaded and available. Enviromnent Variable names are commonly typed IN_ALL_CAPS to distinguish them.
To see what Environment Variables are currently set:
On Linux/Unix systems, open the Bash shell and type: env
On Windows systems, open the Dos shell and type: set
You can also query individual variables if you know their names:
On Linux/Unix systems type: echo $variable_name
On Windows systems type: echo %variable_name%
The Environment Variable's value may be very long and awkward to read in a shell. In that case you can redirect the output to a file and read the file instead.
With Linux/Unix type: echo $variable_name > myFile.txt
With Windows type: echo %variable_name% > myFile.txt
You can get fancy and open a program to view your new file in one step:
echo %CLASSPATH% > myFile.txt | notepad.exe myFile.txt
Setting Environment Variables
Environment Variables can be set temporarily and persistently. Temporary variables exist only within the current shell session. Persistent variables will be available for all subsequent sessions.
Temporary
Adding a temporary variable is handy when you are doing a one time operation. You can simply add a variable by:
Linux type: export MYVARIABLE="Hello there from your environment"
Windows type: set MYVARIABLE="Hello there from your environment"
Now that variable is set and available if you ask for it.
PATH, CLASSPATH and other path-type variables are strings which act as ordered lists to tell programs where to look for resources. The lists are delimited by a colon ":" with Linux and Unix, and a semicolon ";" with Windows. You probably do not want to reassign their values but rather, want to append them. To do this:
Linux/Unix type: export CLASSPATH=$CLASSPATH:/path/to/program
Windows type: set CLASSPATH=%CLASSPATH%;C:\path\to\program
Notice that I added a delimiting character and my new path after the variable.
Persistent Environment Variables
In a lot of cases you will want to use your new Environment settings each time you are at the computer. If you are setting up a Java Environment, for instance, you will want to have the path to Java permanently in your CLASSPATH. This way when you type java myprogram or, if another application needs java, it can find it automatically. Additionally, you can set up your own shortcuts for future use. I use them all of the time to jump from directory to directory or to call commonly used scripts from anywhere in the directory structure.
Your persistent settings are stored in files as strings.
With Unix/Linux, your shell has configuration file(s) in your home directory. Each user has their own configuration settings which may vary widely. For Bash, your settings are saved in either the ".bashrc" file or the ".bash_profile" file. These are hidden files which are read each time you start Bash. Other shells have similar configuration files.
With Windows Single User Systems (Dos, 95, 98, etc), your Environment Variables are stored in your autoexec.bat file. You usually access this file with the command sysedit.
With Windows NT, you access your Environment Variables through the Computer Management console. Selecting "Action -> Properties" selecting the "Advanced" tab and choosing "Environment Variables".
Setting up a Java Environment
For a Java Environment to be setup properly you need to make some additions to your CLASSPATH. Java will function without these settings, however, it will require the full path each time you use it. For the purpose of this example, we will imagine that we have installed the Java jre1.3.1 in this location: (choose your operating system.)
Linux/Unix: /usr/local/jre1.3.1
Windows: C:\jre1.3.1
Within the jre1.3.1 directory is another directory called bin. This directory contains the scripts which call the proper java classes (java, javaw, rmiregistry, etc). We need this directory in our classpath so that our computer knows where to look.
The first change we need to make is to the CLASSPATH. In Linux/Unix and NT there may not be a CLASSPATH set. That is fine. We can easily create one. In some Windows systems the CLASSPATH may already be very long. A CLASSPATH, and other path-type variables, is a delimited list of paths. Do NOT place a delimiter at the end.
If no CLASSPATH exists we simply type our entry into the appropriate file:
For Linux/Unix: export CLASSPATH=/usr/local/jre1.3.1/bin
For Windows: set CLASSPATH=c:\jre1.3.1\bin
That simple! Close your shell and reopen it and the new variable will be read.
If a CLASSPATH exists we will want to add the jre1.3.1/bin to it. Usually we will just add it to the end. Simply use the proper delimiting character followed by the path.
Extending your Java Environment
If you are using a SDK and need to compile Java programs, you will need to place the path to the "tools.jar" file in your CLASSPATH as well. Other Java Packages that you use will need to be referenced as well. Sometimes applications will want a JAVA_HOME variable set. Other times an application will request that the PATH variable contains your java package. This is very simple, just follow the above procedure to create that variable.
There are cases where you have the latest JRE but your browser does not find the Java Plug-in. This frequently happens with Mozilla or Netscape using Linux/Unix. The Java Plug-in for Linux/Unix is located in the JRE/lib directory. Find the exact path to the plugin which is called "javaplugin.so". A link needs to be setup in the "plugin" directory of the browser for the plug-in to work.
Troubleshooting
The most common error is a typo. Check everything carefully. If a letter or path is incorrect it will not work. Case is essential with Unix and is a good habit with Windows. Check to see that your path is set by printing it to the shell or a file as described above.
Type the full path to java and the command -version to see that Java is operating properly. You will receive the version information if it is.
A first entry in the CLASSPATH is a dot. This tells your computer to first look in the current directory, then each of the following directories.
Do not close off the CLASSPATH at the end. The delimiting character is only needed between entries, NOT at the end.
You can have multiple Java Envrionments set up on your system. Make sure that the one you want to call is placed before others in the classpath. The first one found will be used.
Check to see that the files that you are trying to call actually exist. I have seen JDKs installed without a JRE which will cause problems.