Files and Folders location.

Understanding CLASSPATH in Java

  • Java
  • 2 mins read

In this tutorial, you will learn about the CLASSPATH environment variable in Java.

In addition to the PATH environment variable, Java relies on another variable setting: CLASSPATH. The Java runtime environment will look for the user items to execute (Java classes) in the CLASSPATH environment variable.

If it's not set properly, you'll run into lots of problems and go crazy pulling your hair out trying to solve them. The system does not know where to look for system classes, so you have no chance of interfering with that when mucking with the CLASSPATH variable.

Under normal circumstances, you don't have to do anything at all with your CLASSPATH setting. If it's not set, the JRE looks in the current directory for the user classes.

If CLASSPATH is set, the JRE looks in the current directory only if you tell it to, examining just those locations specified by the variable setting. Normally, you want the JRE to look in the current directory because that's where it can find the classes associated with the program you're developing.

Sometimes, previously installed programs will unknowingly set your CLASSPATH for you. If they have, you will need to manually add the current directory back to the search path. A period (.) in the CLASSPATH represents the current working directory, so add it to the variable.

Setting CLASSPATH on Windows Example

On Windows, the CLASSPATH entries are separated by semicolons. Like PATH, setting this up depends on your platform. The new setting will look something like the following, although it depends on what else you have installed for the other entries. The important piece, in any case, is the trailing semicolon and period, as shown here:

SET CLASSPATH=C:\foo\bar;D:\bar\foo.jar;.

CLASSPATH on Linux/Unix Example

For c-shell:

setenv CLASSPATH /usr01/foo:.

For Bourne shell:

CLASSPATH=/usr01/foo:.
export CLASSPATH

Once you have everything set and have rebooted if necessary, you can run the java -version command to see if your PATH is correct and make sure you are using the appropriate version of the Java SDK.

See also: