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.
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.
- Setting classpath as a command line
- Setting classpath as the Environment variable
Method 1: Setting CLASSPATH as a command line
- Each classpath should end with a filename or directory depending on what you are setting the classpath.
- For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file. For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
- For .class files in a named package, the class path ends with the directory that contains the “root” package (the first package in the full package name).
Semi-colons separate multiple path entries. With the set command, it’s important to omit spaces from around the equals sign (=).
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.
- From the desktop, right-click the Computer icon.
- Choose Properties from the context menu.
- Click the Advanced system settings link (a pop-up box will open).
- Click Environment Variables. In the section System Variables, find the CLASSPATH environment variable and select it. Click Edit. (If the CLASSPATH environment variable does not exist, click New and create a new variable with the name CLASSPATH)
- Add all folders separated with separator. Click OK.
- Close all remaining windows by clicking OK.
It is also pictorially depicted below to mind-map clearly in finding the user environment variable window.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.