![]() |
VOOZH | about |
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.util to the current class.
ArrayList<Integer> list = new ArrayList<>() ;
Such that when we call as the JVM knows where to find the class ArrayList. Now, it is impractical for it to go through every folder on your system and search for it. Thus, in java do exists a CLASSPATH variable which is directly used as we provide it the place where we want it to look. Directories and jars are directly put in the CLASSPATH variable.
We can use the - classpath option to set the classpath when calling a JDK tool (the recommended method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can independently set it for each application without impacting other applications and without changing its meaning for other applications.
Methods:
Method 1: Setting CLASSPATH as a command line
Semi-colons separate multiple path entries. With the set command, it's important to omit spaces from around the equals sign (=).
Implementation:
The below specific command is used to set the classpath for any jar files separated by semi-columns.
C:> set CLASSPATH=classpath1;classpath2...
1. C:> set CLASSPATH=.;C:\dependency\framework.jar 2. //Add multiple jars $ set CLASSPATH=C:\dependency\framework.jar;C:\location\otherFramework.jar 3. //* means all the files with .jar extension $ set CLASSPATH=C:\dependency\framework.jar;C:\location\*.jar
Method 2: Setting classpath as the Environment variable
In order to set classpath as the Environment variable, simply find the user environment variables window which as stepwise discussed.
Procedure:
It is also pictorially depicted below to mind-map clearly in finding the user environment variable window.
👁 Image