Different Ways to Set a Classpath in Java
Last Updated :
26 Oct, 2021
Java Virtual Machine(JVM) uses classpath to locate the class files to execute the code. If your classpath is not set and the class file is not present in the same directory as your java file, then JVM will be unable to find the required class file, and it will throw an error (java.lang.ClassNotFoundException).

error caused while JVM is interpreting the code
Ways to Set a Classpath
There are five different ways to set a classpath. These are:
- -cp
- -classpath
- –classpath
- Temporary settings by using the ‘set classpath’ command
- Permanent settings using environment variable window
The limitation of –cp,–classpath, and –class-path methods is that it can set classpath only for the current command line, in the next command line, if we access the desired class directly, we will get an Exception
Syntax:
>java -cp <directory_location> <class name>
Example:
Java
class GFG {
public static void main(String[] args)
{
System.out.println( "GFG!" );
}
}
|

how to set classpath by using -cp command
Command Line Settings –
>java -cp <directory_location> <class name>
OR
>java -classpath <directory_location> <class name>
OR
>java --class-path <directory_location> <class name>
- If we want to access classpath for all command lines, we must set the classpath command option.
- The limitation of the “set classpath” command option is the classpath settings are available only for the current command prompt.
Temporary Settings:
>set classpath=<directory_location>

the temporary setting of classpath using ‘set classpath’ command
If we want to have classpath settings permanently for all the command prompts, we must set classpath in the environment variable section.
Permanent Settings:
- Firstly, Right Click on “This PC”.
- Click Properties.
- Click “Advanced System Settings”.
- Click “Environment Variables”.
- In the “User Variable Section”, click the “New” button.
- Enter Variable name :classpath [Don’t give space between class path] Variable value:<directory_location>(for example in my F:\workspace\bin)
- Click OK->OK->OK.
- Close all windows, open a new command prompt, and run the java command

Environment Variables Setting

permanent classpath settings
We must include .; in the classpath beginning so that the JVM can access both the current working directory and the directory of the desired class file, respectively.
>java -cp <.;directory_location> <class name>
Importance of ‘.’ in the classpath
If we set classpath pointing to one directory, if we don’t place (dot), the classes available in the current working directory will not be found by compiler and JVM. ‘.’ represents the current working directory. The current working directory means not the folder where you save the .java file, the folder path where you open the command prompt.
Syntax to include multiple paths in the classpath
We must separate each folder location with a semicolon separator.
java -cp ./folder1/*;./folder2/*;./folder3/* com.xyz.MainClass
Note – The algorithm followed by the compiler and JVM for finding classes from classpath is First Come First Execute.
Difference between path and classpath searching algorithm priority
- Command prompt software will give first priority to present the working directory. In the present working directory, if the binary file is unavailable, only consider the path variable.
- Compiler and JVM first look for classpath.
- If a classpath is not created, then only they will search in the current working directory.
- If a classpath is created, they don’t look into the current working directory, only the classpath folder.
Note – Generally in the Developer community, the first 4 types are recommended, they try to avoid using the permanent settings using the environment variable window.
Similar Reads
Different Ways to Create Objects in Java
Java is an object-oriented programming language where objects are instances of classes. Creating objects is one of the most fundamental concepts in Java. In Java, a class provides a blueprint for creating objects. Most of the time, we use the new keyword to create objects but Java also offers severa
5 min read
How to Set Classpath in Java?
How to Set Classpath in Java?CLASSPATH describes the location where all the required files are available which are used in the application. Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate the required files. If the CLASSPATH is not set, Java Compiler will not be able to find the
4 min read
Difference Between Path and ClassPath in Java
The ClassPath is a parameter in the Java Virtual Machine(JVM) or the Java compiler that is used by a system or application ClassLoader to locate and load compiled Java bytecodes stored in the ".class" file. On the other hand, The Path is also an environment variable path that behaves as a mediator b
2 min read
How to Set Multiple Classpath in Java in Windows?
In JAVA, you have to set PATH and CLASSPATH variables correctly before running any java program. After installing JDK, the next step is to set the path and CLASSPATH variables in the Windows system environment variable. CLASSPATH is an environment variable which is used by Application ClassLoader to
2 min read
How to Add JAR file to Classpath in Java?
JAR is an abbreviation of JAVA Archive. It is used for aggregating multiple files into a single one, and it is present in a ZIP format. It can also be used as an archiving tool but the main intention to use this file for development is that the Java applets and their components(.class files) can be
4 min read
Difference Between Class.this and this in Java
In java, Class.this and this might refer to the same or different objects depending upon the usage. this this is a reference variable that refers to the current object. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity. Class.this Cla
3 min read
Different Ways To Check Java Version In Windows
Java is a programming language that is platform-independent, popular, simple, secure, and statically typed. Before discussing let's clear Java language needs a runtime platform so before going ahead let's clear how java is present on the machine. Java language comprises three components mainly: Java
3 min read
How to Set Classpath When Class Files are in .jar file in Java?
Classpath is a parameter in JVM or the java compiler that specifies the location of the user-defined classes as well as the packages. While programming in Java, we many times use import statements. Illustration: import java.util.ArrayList; It makes the ArrayList class available in the package java.u
3 min read
Difference Between Static and Non Static Nested Class in Java
Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin
4 min read
How to create a Class in JShell of Java 9
JShell is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and runs from the command line. Jshell have the facility to create a class by which all the efforts can be reduced to write a whol
2 min read