Different Ways to Set Java Classpath Environment Variables in Windows

What is java classpath? We will discuss the different ways to set Java classpath environment variables in windows operating system

What is Java classpath?

The classpath is a purely Java-related environment variable. If our Java application is using third party APIs or user-defined APIs (other than JDK APIs) then those API related directories or jar files must be added to the classpath environment variable to make Java tools recognizing and using the APIs.

Classpath is used for finding library files of software, i.e. the .class file for Java software. Classpath is a mediator between the programmer and the compiler. The programmer will set the classpath variable by storing the software libraries’ file path. JVM needs a classpath variable while executing our class file for finding library files that are used from our class.

Why do we need to set Java classpath?

Take an example, the .class file is not in the current working directory. Now, can we execute it from the current working directory? No, because by default JVM search for the .class file in the current working directory only. If classes are saved in a different directory, for executing them from the current directory we must set the classpath.

It is used for setting the path for classes when they are available in a different directory. When we set classpath then JVM also searches the file in the given classpath, only not in the current working directory.

If we want to access classes stored in one folder from another folder then, we must set classpath by storing this folder path. If we don’t set classpath then we can get an error “Could not find symbol class” or “Java.lang.classNotFoundException” or “Could not find or load main class”.


Different ways to set java classpath

There are four different ways to set Java classpath.

1) Temporary settings using java command options
2) Temporary settings for one cmd
3) Permanent settings by using environment settings (Only for one user)
4) Permanent settings by using system settings (For all users)


Set java classpath using the java command options

The java commands options to set Java classpath for one command prompt are:-

-cp or, -classpath or, --class-path

Demonstrating the java command options

To test these java command options, write simple HelloWorld program, compile it.

 //HelloWorld.java
 public class HelloWorld {
     public static void main(String[] args){
         System.out.println("Hello World");
     }
 }

Compile the above file with below command, it will save the .class file in the “C:\Testing\JavaProgram” folder.

> javac -d C:\Testing\JavaProgram HelloWorld.java

Note:- If you are working with JDK version 1.0 to 1.8 then you must explicitly create the folders “Testing”, and “JavaProgram” inside the C drive, then only .class file will be saved in given folder else compiler will throw below errors

javac: directory not found:  C:\Testing\JavaProgram
Usage: javac <options> <source files>
use -help for a list of possible options

From Java 9.0 version onwards we no need to create the folder explicitly. If the folder is not available then the compiler we create it.

Now open command prompt window, and it executes the HelloWorld.class from any location by using java command options,

> java -cp C:\Testing\JavaProgram HelloWorld
Hello World

> java -classpath C:\Testing\JavaProgram HelloWorld
Hello World

> java --class-path C:\Testing\JavaProgram HelloWorld
Hello World

The output will be the same for the above three command options.

Among four ways to set java classpath, this way is a commonly used approach because dependent classes are varied from command to command.

Limitation of setting java classpath using java command options

By using java command options -cp / -classpath / –class-path, we can set classpath only for current command line (single command line only). In the next command line, again we need to set the classpath. In the next command line, if we execute class directly then we will get an exception: class not found exception

> java -cp C:\Testing\JavaProgram HelloWorld
Hello World

> java HelloWorld
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

Set Java classpath for one command prompt window

We can set java classpath for one command prompt window. For this use below command,

> set classpath=C:\Testing\JavaProgram;%classpath%;.

> java HelloWorld
Hello World

Limitation:- Using this temporary setting we can set classpath only for one command prompt window. In that command prompt window, we can access classpath from all command lines. But, if we open another command prompt window then we can’t access those settings for the classpath. If we close the current command prompt window (where the temporary setting for classpath was done), then we can’t access that classpath. Again, we need to set the classpath.

If we want to make path settings to all cmd windows, we must set a classpath setting permanently.


Permanent settings for the user

Go to My Computer -> Properties -> Advance System Settings -> Environment variables. In the user variables section, click on the new button (If the classpath variable is already available then click on “edit” and please don’t disturb the old values.)

set java classpath using user variable

Creating a new variable,

Variable Name: CLASSPATH
Variable Value: C:\Testing\JavaProgram;.
create path for JDK

If you are updating/editing the classpath variable value then add the C:\Testing\JavaProgram at the beginning and don’t disturb the old values, leave them to remain as it is.

Variable Name: CLASSPATH
Variable Value: C:\Testing\JavaProgram;<Old_values>;.

In both of the case don’t forget to add ;. at last. It is a good practice to add the dot (.) at the last. Here the semicolon (;) is a separator, and dot (.) represents the current directory.

Open a new command prompt and execute the HelloWorld.class file using java tool.

> java HelloWorld
Hello World

Now, the JVM will check .class in the all classpath folders. In our case the HelloWorld.class is available in the “C:\Testing\JavaProgram” and this value is at the beginning of the variable value. So, JVM will execute it.

Form anywhere and any command prompt, we can execute all .class files which are inside the C:\Testing\JavaProgram. This permanent setting will be preserved after the system restart also.

Limitation of setting Java classpath in user variables

This classpath setting will be available for a single user of the computer system. Only that user can access those files for which users these are done. If your computer has only one user then you can happily use these settings. But for more than one user, we should go next setting:- Permanent settings for the system.


Permanent classpath settings for the system

Go to My Computer -> Properties -> Advance System Settings -> Environment variables. In the system variables section, click on the new button (If the classpath variable is already available then click on “edit” and please don’t disturb the old values.)

The settings are similar as we have done in for the user variables.

Creating a new variable,

Variable Name: CLASSPATH
Variable Value: C:\Testing\JavaProgram;.

If you are updating/editing the classpath variable value then add the C:\Testing\JavaProgram at the beginning and don’t disturb the old values, leave them to remain as it is.

Variable Name: CLASSPATH
Variable Value: C:\Testing\JavaProgram;<Old_values>;.

In both of the case don’t forget to add ;. at last. Now, the classpath setting is available for all the users of the computer. All the users of the computer can access the classpath settings.

Q) If the classpath setting is done by both permanent and temporary, then the first priority will be given to whom?
Ans:- Temporary settings.

Q) When both system and user settings are done then first priority will be given to whom?
Ans:- User setting. When the user variables setting is available for the java classpath then system variables setting will not be taken.

Q) Among the four approaches, which is recommended?
Ans:- When we are installing permanent software in one system then the permanent setting for system variables is recommended.


Importance of adding current directory in Java classpath

While setting classpath we had given dot at the end of the variable values, the dot represents the current directory. The adding current directory to the classpath is very important. The JVM searches for .classes only the locations given in the classpath settings.

Take an example, do classpath settings and don’t add the current directory (dot and semicolon) at the last. Now, develop a Java application, compile it and try to execute it.

Is the .class file executed successfully? No. You will get an exception: Could not find or load main class Test

Do the below settings in user variables.

Variable Name: CLASSPATH
Variable Value: C:\Testing\JavaProgram
 //Test.java
 public class Test {
     public static void main(String[] args){
         System.out.println("Test class");
     }
 }
> javac Test.java
> java Test
Error: Could not find or load main class Test

Why this error is coming? The JVM searches for the Test.class file only inside the “C:\Testing\JavaProgram” folder, not in the current working directory. The Test.class is not available in the “C:\Testing\JavaProgram” folder so JVM gives exception: Could not find or load main class Test

Now, add “;.” in the classpath and open new command prompt and try to execute the Test.class. This time JVM executes Test.class files successfully.

Variable Name: CLASSPATH
Variable Value: C:\Testing\JavaProgram;.

Open new command prompt window, Go to the folder where Test.class is available and execute it using java tool.

> java Test
Test class

If we are adding current directory (dot) in the classpath variable value then compiler first search the Test.class in the “ C:\Testing\JavaProgram”. If it is available then JVM will execute it else the JVM will search into the second value folder. Here the second value is the current working directory (dot). So, the JVM will search for the “Test.class” in the current working directory, it is available hence JVM will execute it.


When do we need to set Java classpath?

As you are working on the Java domain. Later whenever you install software that is used for Java application development in that situations you need to set the classpath.

For example, you want to develop a servlet application (Java-based web-application which is used to develop websites), in that case, servlet API files should be accessible to the JDK and it can be done by using setting classpath.

There are many similar situations when you need to set the classpath. Another example is of working with JDBC application, in that case, jar file(collection of java files like a zip file) of oracle driver should be accessible to the JDK.


Important points on environment variables

1) The environment variables are not case-sensitive. So, classpath/CLASSPATH/ClassPath, etc all are allowed.

2) The values added from advanced system settings will remain permanent whereas the values added to the command prompt will remain temporary.

3) The user environment variables settings are specified to currently logged in window users, whereas the system environment variables settings are available for all the users of the windows operating system.

4) Multiple values added to the environment variables must be separated by using the semicolon.

5) The values added to the environment variables will not reflect to the old command prompts. We must open a new command prompt window.

6) It is recommended to add new values to the environment variable at the beginning of the existing values. If we are adding the new values at the end of the environment variable and one value at the middle is broken/wrong then JVM stop searches after that middle value. Hence it is always recommended to add new values at the beginning.


Difference between path and classpath in Java

We have discussed path and classpath in Java. Now, we will see the difference between path and classpath in Java.

The path is used by a command prompt (OS) for finding software binary files. It is used for accessing binary files of the software from different directories of the operating system irrespective of the directory on which they are installed.

Classpath is used by compiler and JVM for finding .class files (libraries and API) when classes are required to access from different directories of the operating system. We must set classpath by saving the folder path address in which .class files are available.

pathclasspath
It is used for locating binary files of software.It is used for locating library files of software.
It stores the bin folder path.It stores the lib/jar folder path.
It is used by CMD for finding binary files from different software configured in path variable.It is used by compiler and JVM for finding library files of different software configured in the classpath variable.
It is meant for all software installed on our computer including OS for finding binary files.It is meant for only Java and its related software, but not for all software.
If ‘_ _ _ _ _’ is not recognized error occurs then we should set path.If we get “Can not find symbol” or “CouldNotFoundExecption error” or “could not load main class error” then we must set the classpath.
.; (dot semicolon) is not required..; (dot semicolon) is required.

Setting Classpath vs copying the file to the ext folder

There is one alternative for classpath. Copy the file and paste it into the <JAVA_HOME>\jre\lib\ext folder.

After jdk1.8 there was no JRE folder given explicitly in the JAVA_HOME, discussion about copying the file into the ext folder is worthless. But currently many technology working on jdk1.8 and in jdk1.8 ext folder is available, hence we should discuss this point.

At the time of JDK installation, we set the path variable for JDK, so all files of JAVA_HOME are available for javac and java tools. When we copy our file into the ext folder then it will be treated as the file of JDK software. In this way, we can access the file, no need to set the classpath. But it is not recommended to copy the file to the ext folder.

Table: java classpath vs ext folder

Classpath Copying file to the ext folder
1) We can add both directories and jar files. 1) We can add only jar files. We are not allowed to add folder/directories to the ext folder, it will not work.
2) It works in all version of Java 2) It works only up to Java8
3) The modification done here will reflect only to the new command prompt. 3) The modifications will reflect in both old and new command prompt.
4) The values added in the classpath will be used by multiple JDK versions or different software installed on a computer. 4) For every version, we must add separately.
5) Values added here will be used by ApplicationClasssLoader 6) Values added here will be used by Extension ClassLoader
7) It refers to the value of its original location. No physical copy takes place. 7) It takes a copy of the original value to the ext folder.

If the file is of big size 1GB/2GB then copying the file into the ext folder is not a good approach, we are just westing the space.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply