When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:

import org.javaguy.coolframework.MyClass;

Or sometimes you 'bulk import' stuff by saying:

import org.javaguy.coolframework.*;

So later in your program when you say:

MyClass mine = new MyClass();

The Java Virtual Machine will know where to find your compiled class.

It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

So, classpaths contain:

  • JAR files, and
  • Paths to the top of package hierarchies.

How do you set your classpath?

The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:

export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.

The second way is to use the -cp parameter when starting Java, like this:

java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/"  MyMainClass

A variant of this is the third way which is often done with a .sh or .bat file that calculates the classpath and passes it to Java via the -cp parameter.

There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (:) is the classpath separator. On Windows the separator is the semicolon (;)

So what's the best way to do it?

Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the CLASSPATH environment variable so one program works, and you end up breaking another program.

The -cp is the way to go. I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).

Answer from bokmann on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › classpath-in-java
CLASSPATH in Java - GeeksforGeeks
October 6, 2021 - We put directories and jars in the CLASSPATH variable. Let's say the above package resides in the directory dir. The complete path of the Menu class file would be dir/org/company/Menu. We'll specify only the directory dir in our classpath variable, as the rest information regarding the path is provided by the import statements.
Top answer
1 of 12
910

When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:

import org.javaguy.coolframework.MyClass;

Or sometimes you 'bulk import' stuff by saying:

import org.javaguy.coolframework.*;

So later in your program when you say:

MyClass mine = new MyClass();

The Java Virtual Machine will know where to find your compiled class.

It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

So, classpaths contain:

  • JAR files, and
  • Paths to the top of package hierarchies.

How do you set your classpath?

The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:

export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.

The second way is to use the -cp parameter when starting Java, like this:

java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/"  MyMainClass

A variant of this is the third way which is often done with a .sh or .bat file that calculates the classpath and passes it to Java via the -cp parameter.

There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (:) is the classpath separator. On Windows the separator is the semicolon (;)

So what's the best way to do it?

Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the CLASSPATH environment variable so one program works, and you end up breaking another program.

The -cp is the way to go. I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).

2 of 12
127

Think of it as Java's answer to the PATH environment variable - OSes search for EXEs on the PATH, Java searches for classes and packages on the classpath.

People also ask

What is classpath?
Classpath is a parameter used by the Java compiler and JVM (Java Virtual Machine) to determine the location of class files required for compiling and running Java applications. It can point to directories, JAR files, or other resources containing bytecode. Classpath ensures the program can locate all necessary dependencies. It can be set as an environment variable or explicitly provided on the command line during execution.
🌐
lenovo.com
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo ...
What is the purpose of setting a classpath in Java?
The main purpose of setting a classpath is to inform the Java compiler and JVM where to find the necessary class files for a program to function correctly. If a program depends on external libraries or custom classes, classpath acts as a roadmap, ensuring all dependencies are accessible during compilation and runtime. Properly configured classpath prevents runtime errors caused by missing or incorrectly referenced resources.
🌐
lenovo.com
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo ...
Does classpath work differently in modular Java applications?
Yes, modular Java applications introduced with Java 9 use the module path, which is separate from classpath. The module system offers more control over dependencies and their accessibility. Even though classpath can still be used for backward compatibility or non-modular code, switching to the module path is recommended for modern projects to leverage modular features.
🌐
lenovo.com
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo ...
🌐
NTU
www3.ntu.edu.sg › home › ehchua › programming › java › J9c_PackageClasspath.html
Packages & CLASSPATH in Java
Clearly, the "dot" in the package name corresponds to a sub-directory of the file system. The base directory ($BASE_DIR) could be located anywhere in the file system. Hence, the Java compiler and runtime must be informed about the location of the $BASE_DIR so as to locate the classes. This is accomplished by an environment variable called CLASSPATH...

When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:

import org.javaguy.coolframework.MyClass;

Or sometimes you 'bulk import' stuff by saying:

import org.javaguy.coolframework.*;

So later in your program when you say:

MyClass mine = new MyClass();

The Java Virtual Machine will know where to find your compiled class.

It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

So, classpaths contain:

  • JAR files, and
  • Paths to the top of package hierarchies.

How do you set your classpath?

The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:

export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.

The second way is to use the -cp parameter when starting Java, like this:

java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/"  MyMainClass

A variant of this is the third way which is often done with a .sh or .bat file that calculates the classpath and passes it to Java via the -cp parameter.

There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (:) is the classpath separator. On Windows the separator is the semicolon (;)

So what's the best way to do it?

Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the CLASSPATH environment variable so one program works, and you end up breaking another program.

The -cp is the way to go. I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).

Answer from bokmann on Stack Overflow
🌐
Medium
medium.com › javarevisited › back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - ClasspathProject/src/java/myprogram/ | ---> Main.java | ---> utils/ | ---> Util.java · And here are the source files just so you know what we are dealing with. ... I will be using this structure for the following parts as well so If you want to follow along I suggest you create it as well before proceeding. If you are new to java it can be helpful to know that packages are generally suppose to be mirrored by the file structure.
🌐
Wikipedia
en.wikipedia.org › wiki › Classpath
Classpath - Wikipedia
May 30, 2026 - The classpath tells Java where to look in the filesystem for files defining these classes. The virtual machine searches for and loads classes in this order: bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional). extension classes: packages ...
🌐
UCSB
sites.cs.ucsb.edu › ~cappello › 50-2005-Winter › lectures › classpath.html
Setting the class path
For example, suppose you want the Java runtime to find a class named Cool.class in the package utility.myapp. If the path to that directory is /java/MyClasses/utility/myapp, you set the class path so that it contains /java/MyClasses. To run that app, you could use the following JVM command: % java -classpath /java/MyClasses utility.myapp.Cool
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - For example, suppose you want the Java JRE to find a class named Cool.class in the package utility.myapp. If the path to that directory is C:\java\MyClasses\utility\myapp, then you would set the class path so that it contains C:\java\MyClasses. To run that application, you could use the following java command: java -classpath C:\java\MyClasses utility.myapp.Cool
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath.html
Setting the class path
For example, suppose you want the Java runtime to find a class named Cool.class in the package utility.myapp. If the path to that directory is /java/MyClasses/utility/myapp, you would set the class path so that it contains /java/MyClasses. To run that app, you could use the following JVM command: % java -classpath /java/MyClasses utility.myapp.Cool
🌐
C# Corner
c-sharpcorner.com › UploadFile › fd0172 › understanding-classpath-concept-in-java
Understanding ClassPath in Java
October 9, 2019 - ClassPath is used to tell the compiler that we are using another class contained in the path we provided. Define package p1 and class A as. This class is saved in the "E:\Sandy\java programs\p1" folder.
🌐
Lenovo
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo US
Classpath is a key component in traditional Java applications, used to locate and load class files and libraries required during runtime. It serves as a mechanism for defining the locations where the Java Virtual Machine (JVM) should look for user-defined classes and packages.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › technotes › tools › windows › classpath.html
Setting the class path
For example, suppose you want the Java runtime to find a class named Cool.class in the package utility.myapp. If the path to that directory is C:\java\MyClasses\utility\myapp, you would set the class path so that it contains C:\java\MyClasses. To run that app, you could use the following JVM command: C:> java -classpath C:\java\MyClasses utility.myapp.Cool
🌐
Javatpoint
javatpoint.com › how-to-set-classpath-in-java
How to Set CLASSPATH in Java - Javatpoint
August 6, 2014 - CLASSPATH is an environment variable which is used by Application ClassLoader to locate and load the .class files. The CLASSPATH defines the path, to find third-party and user-defined classes that are not extensions or part of Java platform. Include all the directories which contain .class ...
🌐
Fiu
users.cs.fiu.edu › ~downeyt › webdev › packages.html
How Java uses Packages and CLASSPATH
If a class file is in a package named jbond007, then Java will look in all the paths in the CLASSPATH with jbond007 appended to them.
🌐
Scaler
scaler.com › home › topics › what is java classpath?
What is Java CLASSPATH? - Scaler Topics
March 27, 2024 - A NullPointerException will be raised if we pass a null argument to a constructor or method in any class or interface in this package. All of the classes named below are imported; if you only wish to import one, follow the instructions below. The class Menu can be located by the JVM. How will the JVM now be aware of this location? It is not practical for it to look through each folder on your machine. In order to provide it the location where we want it to look, we use the CLASSPATH variable.
🌐
IBM
ibm.com › docs › en › i › 7.4.0
Java classpath - IBM Documentation
October 7, 2025 - The classes directory in the Product directory in the "root" (/) file system. Some Java tools and commands contain a classpath parameter in which a list of path names can be specified.
🌐
Study.com
study.com › business courses › java programming tutorial & training
What is Classpath in Java? - Definition & Example - Lesson | Study.com
March 20, 2019 - CLASSPATH is actually an environment variable in Java, and tells Java applications and the Java Virtual Machine (JVM) where to find the libraries of classes. These include any that you have developed on your own.
🌐
Codemia
codemia.io › home › knowledge hub › what is a classpath and how do i set it?
What is a classpath and how do I set it? | Codemia
January 26, 2025 - In Java, the classpath is a parameter that allows the Java runtime and Java compiler (javac) to locate the required classes and packages needed by programs at runtime or compilation time.
🌐
How to do in Java
howtodoinjava.com › home › java basics › java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - Use the . (dot) to include the current path in the classpath where the .class file has been generated. $ javac –classpath C:\dependency\framework.jar MyApp.Java $ java –classpath .;C:\dependency\framework.jar MyApp