Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Set Classpath When Class Files are in .jar file in Java?

  • Last Updated : 11 Feb, 2021

Classpath is a parameter in JVM or the java compiler that specifies the location of the userdefined classes as well as the packages. While programming in Java, we many times use import statements. 

Illustration:

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.

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:

  1. Setting classpath as a command line
  2. 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 (=).

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:

  1. From the desktop, right-click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link (a pop-up box will open).
  4. 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)
  5. Add all folders separated with separator. Click OK.
  6. Close all remaining windows by clicking OK.

It is also pictorially depicted below to mind-map clearly in finding the user environment variable window.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!