Computer Careers Logo

What is this good for?

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.

Learning Objective

Be able to set the CLASSPATH variable correctly in Windows so Java programs will run.


Tutorial

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:

No Class Def Found error message

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.

contents of test folder on C: drive


Check your existing CLASSPATH

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.

output from SET command


Test the CLASSPATH

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

adding to the CLASSPATH variable

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.


Setting the Environment Variable in Windows XP

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.

  1. Right-mouse click on My Computer on your desktop and select Properties.
  2. In the System Properties window click on the Advanced tab.
  3. System Properties - Advanced tab

  4. Click on the Environment Variables button near the bottom.
  5. environment variable window
  6. Highlight the CLASSPATH if there is one and click the Edit button. Here you can see where I added C:\test; on the end of the CLASSPATH. I always finish things by adding the semi-colon ";"
  7. editing the classpath

  8. Click okay and try running your Java files. If you still get an error, go back in and verify that you spelled everything correctly. One single typo will cause things to break. I always follow upper/lower case rules as well although this isn't necessary in the Windows environment because to windows everything is CAPS. It is good programming practice.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.


Questions? Send me an email: peter.johnson@southcentral.edu

Read my blog at: http:webexplorations.com/blog

South Central College Logo

Strong Start, Successful Finish