The CLASSPATH environment variable is used by Java to find .class files on your hard drive. It is very similar to the path environment variable used by DOS to find programs. Knowing how to set the CLASSPATH can make a difference if your Java programs run or not.
Be able to set the CLASSPATH variable correctly in Windows so Java programs will run.
When you run a Java file from the command line Java looks in the current directory for the class file. If it is not found Java uses the CLASSPATH environment variable to determine where else to look on your hard drive.
As an example I tried to run a simple hello.class program from the root directory of my hard drive. Here is what Java told me:
In the following steps we will set up the CLASSPATH variable which will allow Java to find this program.
Here's the class file Hello.class (right-mouse click, "save link as") if you want to download it and put it into a directory on your hard drive. I created a folder in my C:\ drive named "test" and put the class file in there if you want to follow these steps exactly. However, you can put the file anywhere you like, just adjust the folder name in the following steps.
You can check your existing CLASSPATH by typing in SET in the command
window.
Here is a shot of my class path. I've highlighted it to help you
find it in the results. The single dot at the beginning means "look
in the current directory". Then each directory Java should look in
is listed, separated by a semi-colon ";" The tildes "~" are how
Windows compresses a long file name into the 8-character requirement
of the command window.
You can manually tell the operating system to set the CLASSPATH by using this command:
SET CLASSPATH=.;%CLASSPATH%;C:\test;
First there is the dot for "current directory", than the %CLASSPATH% variable which will insert your current classpath, and then the path you want to add. In this case my test folder which contains the Hello.class file.
Now run the program: java Hello
What most people do is put these two statements in a BAT file (or script on Linux) and use that to run the Java program. The advantage of this is you aren't changing the environment variable on the system.
But, you may want to make this setting more permanent. You can do this by setting the environment variable. This also lets you change the order of your CLASSPATH so Java finds some programs sooner without looking through all those folders. But be careful! One wrong character and several programs could break because they can't be found by Java. At the very least do a screen dump (hit PRTSCRN and then CTRL V while inside a graphic program or a document in Word) of your existing CLASSPATH so you can always retype it in if you have to.

Questions? Send me an email: peter.johnson@southcentral.edu
Read my blog at:
http:webexplorations.com/blog
Strong Start, Successful Finish