How to set classpath in Java - Windows, and Linux

The classpath is the path that the Java runtime environment searches for classes and other resource files. The class search path ("classpath") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

During runtime of any Java application, the CLASSPATH is a parameter that tells the JVM where to look for classes and packages. In detail, application class loader will always scan the jar files and classes at specified paths in this variable.

The default value of the classpath is "." (dot), meaning that only the current directory is searched for dependencies. Specifying either the CLASSPATH environment variable or the -cp command line switch overrides this value.

The order in which you specify multiple classpath entries is important. The Java interpreter will look for classes in the directories in the order they appear in the classpath variable.

1). Update the PATH Environment Variable (Microsoft Windows)

You can run Java applications just fine without setting the PATH environment variable. Or, you can optionally set it as a convenience.

Set the PATH environment variable if you want to be able to conveniently run the executables (javac.exe, java.exe, javadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:\Program Files\Java\jdk1.8.0\bin\javac MyClass.java

The PATH environment variable is a series of directories separated by semicolons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should have only one bin directory for the JDK in the path at a time (those following the first are ignored), so if one is already present, you can update that particular entry.

The following is an example of a PATH environment variable:

C:\Program Files\Java\jdk1.8.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem

It is useful to set the PATH environment variable permanently so it will persist after rebooting. To make a permanent change to the PATH variable, use the System icon in the Control Panel. The precise procedure varies depending on the version of Windows:

Windows 7 8 or 10:

From the desktop,

 Right-click the My PC or Computer icon.

Choose Properties from the context menu.

Control Panel System


Click the Advanced system settings link.

 
system properties

Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.

Path Variables




In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. Click OK. Close all remaining windows by clicking OK.

%JAVA_HOME%\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem

Variables enclosed in percentage signs (%) are existing environment variables. If one of these variables is listed in the Environment Variables window from the Control Panel (such as JAVA_HOME), then you can edit its value.

java path variable

 

If it does not appear, then it is a special environment variable that the operating system has defined. For example, SystemRoot is the location of the Microsoft Windows system folder. To obtain the value of an environment variable, enter the following at a command prompt. (This example obtains the value of the SystemRoot environment variable)

echo %SystemRoot%

2). Update the PATH Variable (Solaris and Linux)

You can run the JDK just fine without setting the PATH variable, or you can optionally set it as a convenience. However, you should set the path variable if you want to be able to run the executables (javac, java, JavaDoc, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

% /usr/local/jdk1.8.0/bin/javac MyClass.java

To find out if the path is properly set, execute:

% java -version

This will print the version of the java tool if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.

To set the path permanently, set the path in your startup file.

For C shell (csh), edit the startup file (~/.cshrc):

set path=(/usr/local/jdk1.8.0/bin $path)

For bash, edit the startup file (~/.bashrc):

PATH=/usr/local/jdk1.8.0/bin:$PATH export PATH

For ksh, the startup file is named by the environment variable, ENV. To set the path:

PATH=/usr/local/jdk1.8.0/bin:$PATH export PATH

For sh, edit the profile file (~/.profile):

PATH=/usr/local/jdk1.8.0/bin:$PATH export PATH

Then load the startup file and verify that the path is set by repeating the java command:

For C shell (csh):

% source ~/.cshrc % java -version For ksh, bash, or sh: % . /.profile % java -version

3). Checking the CLASSPATH variable (All platforms)

The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)

The preferred way to specify the classpath is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care.

The default value of the classpath is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

To check whether CLASSPATH is set on Microsoft Windows NT/2000/XP, execute the following:

C:> echo %CLASSPATH%

On Solaris or Linux, execute the following:

% echo $CLASSPATH

If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) or simply %CLASSPATH% (Microsoft Windows NT/2000/XP).

To modify the CLASSPATH, use the same procedure you used for the PATH variable.

Classpath wildcards allow you to include an entire directory of .jar files in the classpath without explicitly naming them individually. For more information, including an explanation of classpath wildcards, and a detailed description on how to clean up the CLASSPATH environment variable, see the Setting the Class-Path technical note.

4). Setting CLASSPATH from Command Line

Use -classpath argument to set classpath from command prompt/console.

Suppose we have a folder named dependency where JAR files and other classes are placed.

Add a single jar file in the classpath

Below syntax examples will add a single jar file in the classpath.

Add single jar

//Windows $ set CLASSPATH=.;C:\dependency\yourlib.jar //Linux $ export CLASSPATH=.:/dependency/yourlib.jar

5). Add multiple jar files in the classpath

Sometimes we need to add multiple jars in the classpath.for that we need to use the delimiter for the operating system (either; or :) as a separator between the locations specified for the CLASSPATH.

To add all JAR files present in a directory, use wildcard character ('*').

Add multiple jars //Windows $ set CLASSPATH=C:\dependency\yourlib1.jar;C:\location\yourlib1.jar $ set CLASSPATH=C:\dependency\yourlib.jar;C:\location\*.jar //Linux/Unix $ export CLASSPATH=/dependency/framework.jar:/location/yourlib2.jar $ export CLASSPATH= /dependency/framework.jar:/location/*.jar

6). Add multiple classes to the classpath

In some situations, we need to add a folder where our java classes are present. for that, we need to add the folder where the class file is present. e.g. suppose we have many .class files are present in the location folder which you want to include in the classpath.

Add classes

//WINDOWS $ set CLASSPATH=C:\dependency\*;C:\jarlocation //Linux/Unix $ export CLASSPATH=/dependency/*:/jarlocation

As a best practice, always organize all JAR files and application classes inside one root folder. This may be the workspace for the application.

7). Find and Print CLASSPATH Value

Using the echo command we can find all path entries in the CLASSPATH variable

//Windows c:/> echo %CLASSPATH% //Linux/Unix $ echo $CLASSPATH

If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) console or simply %CLASSPATH% printed in windows command prompt.

In this article, we have seen  How to set classpath in Java - Windows, and Linux.