How to Set Classpath for Java in Windows

Classpath in Java refers to the path which will be used by the classloaders to load class in Java. If the classpath is not set correctly you are more likely to get ClassNotFoundException or NoClassDefFoundError exceptions. Classpath can be specified by using CLASSPATH variable in the Environment variable and using -cp or -classpath command line option. In this tutotrial we will learn how to set Classpath in Environment variable as well as command line.

Setting classpath for Java in Windows – Environment Variable

In order to set the class for Windows (Windows 7, Windows XP etc) we need to specify the CLASSPATH variable in the Environment Variable, The CLASSPATH variable is not case sensitive. Follow the below steps to set classpath

Set Classpath in Windows XP

  • Right click on My Computer and goto properties (or) Press Windows + Pause to open up System Properties . Now traverse to Advanced Tab and click on “Environment Variable”.
  • If the classpath  already exist in System Variable, then put a semi-colon(;) at the end and add the Path till lib folder eg : “C:\Program Files\Java\jdk1.7.0_75\lib”
  • If the classpath  doesn’t exist in System Variable, then under System Variable click on New give Variable Name as “CLASSPATH” and Variable Value as “C:\Program Files\Java\jdk1.7.0_75\lib”
  • In order to check the classpath which is set, type echo %CLASSPATH% in command prompt, it will display the CLASSPATH which is set.

Set Classpath in Windows 7 or Windows 8

Setting up classpath in Windows 7 or Windows 8 is almost the same as Windows XP  with some slight changes.

  • Right click on Computer and goto properties (or) Press Windows + Pause to open up System prompt. In that Click on the “Advanced System Settings” to open System Properties.
  • Now traverse to Advanced Tab and click on “Environment Variable”.
  • If the classpath  already exist in System Variable, then put a semi-colon(;) at the end and add the Path till lib folder eg : “C:\Program Files\Java\jdk1.7.0_75\lib”
  • If the classpath  doesn’t exist in System Variable, then under System Variable click on New give Variable Name as “CLASSPATH” and Variable Value as “C:\Program Files\Java\jdk1.7.0_75\lib”

Add CLASSPATH enviroment variable via command line

you can set the CLASSPATH using command line using the below command. If there is no CLASSPATH already exist the we can use below command.

SET CLASSPATH=.;C:\Program Files\Java\jdk1.7.0_75\lib

Point to be noted is that we need to have a ‘.;’ before our actual path to denote our current directory. Since JDK 1.3, if no CLASSPATH is set explicitly, the default is set to the current working directory ‘.’. However, if you explicitly set your CLASSPATH, you have to include the current directory ‘.’ explicitly. Otherwise, the current directory will not be searched.

If a CLASSPATH entry already exist then we need to include it along with our path, by running the below command

SET CLASSPATH=%CLASSPATH%;C:\Program Files\Java\jdk1.7.0_75\lib

Setting classpath for Java in Windows – Command Line

In order to set classpath for Java via Command Line we will be using the command -classpath or -cp.

If we need to set classpath for a jar and class

java -classpath “Spring.jar” MyClassName

Set classpath for multiple jars in the same directory.

java -classpath .; \*;

Leave a Reply

Your email address will not be published. Required fields are marked *