Different Ways to Set a Classpath in Java - GeeksforGeeks

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
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
// This code is located in F:\workspace\src// It's class file is located in F:\workspace\binclass GFG {    public static void main(String[] args)    {        System.out.println("GFG!");    }}
how to set classpath by using -cp command
>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.
>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. 
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
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>
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. 
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.
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. 
java-basics
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Comparator Interface in Java with Examples
Strings in Java
HashMap get() Method in Java
StringBuilder Class in Java with Examples