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
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.

🌐
Reddit
reddit.com › r/learnjava › what exactly is the classpath? i don't fully get it and only know that it's akin to the path variable in linux.
r/learnjava on Reddit: What exactly is the classpath? I don't fully get it and only know that it's akin to the PATH variable in Linux.
September 6, 2023 -

I read this:

In Java, the term "classpath" refers to a parameter that specifies a set of directories or JAR (Java Archive) files where the Java Virtual Machine (JVM) and Java compiler should look for classes and resources when running or compiling Java applications

But where does this classpath comes into existence? If you for example have a completely vanilla project with some source code, and want to compile it manually with javac, does this notion of class path exist, for example?

How does it work in the context of build automation tools like Maven?And in an intelliJ project, for example, where can I find the class path and how does it work relatively to the project?

I've read a lot about what it does, but not what it exactly is and I would really appreciate some clarification!

Edit: I tried to look at the docs (oracle for example), but even there it's all kind of vague.

Top answer
1 of 3
3
Check out the answers in here : https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
Discussions

What is a classpath in Java, and how do I set it? - Ask a Question - TestMu AI (formerly LambdaTest) Community
I came across this line: “The first thing the format() method does is load a Velocity template from the classpath named output.vm.” What does classpath mean in this context, and how do I properly set the Java classpath to ensure that my application can locate the necessary resources? More on community.testmuai.com
🌐 community.testmuai.com
0
March 11, 2025
What exactly is the classpath? I don't fully get it and only know that it's akin to the PATH variable in Linux.
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
5
11
September 6, 2023
What is the linking/classpath exception in the OpenJDK GPL License?
The main difference between the LGPL and the GPL-with-classpath-exception is that, in the former case, you are required to provide the end-user with the means to link your application with an edited version of the library instead, and to allow any required reverse-engineering to debug that kind of change. The end user should be able to drop-in his own .class files or JARs to replace the LGPL'd library without breaking the application, which means you can't obfuscate that interface (or, if you do, you have to provide the obfuscation mapping file). More on reddit.com
🌐 r/java
3
12
January 10, 2016
What is wrong with this code?
Yea, be sure to build a fat jar More on reddit.com
🌐 r/aws
9
0
October 27, 2019
People also ask

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 ...
Can I use classpath to include external libraries in a Java project?
Yes, external libraries, often packaged as JAR files, are included in a Java project by adding their paths to the classpath. This allows the compiler and JVM to locate and load the necessary resources. Properly referencing these libraries ensures the application can seamlessly use external functionalities like database connectivity, logging frameworks, or UI tools. Properly referencing these libraries ensures the application can seamlessly use external functionalities like database connectivity, logging frameworks, or UI tools.
🌐
lenovo.com
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo ...
What is the role of the -cp or -classpath option in Java commands?
The -cp or -classpath option allows developers to specify a custom classpath directly on the command line when compiling or running Java programs. It overrides any classpath environment variable, ensuring the provided paths are used for locating class files and resources. This flexibility is useful for running specific programs or testing setups without changing system-wide settings.
🌐
lenovo.com
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo ...
🌐
Medium
medium.com › javarevisited › back-to-the-basics-of-java-part-1-classpath-47cf3f834ff
Tutorial on how Java classpath works | Javarevisited
May 15, 2022 - However, it is required by the java command to run. Let’s compile this little project. First we create a bin folder to hold the compiled stuff in the root project folder then compile the two files [2]. > mkdir bin > javac src/java/myprogram/Main.java src/java/myprogram/utils/Util.java -d bin/ You should now have the following file structure. ClasspathProject/ | ---> src/java/myprogram/ | ---> Main.java | ---> utils/ | ---> Util.java | ---> bin/myprogram/ | ---> Main.class | ---> utils/ | ---> Util.class
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › paths.html
PATH and CLASSPATH (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) or simply %CLASSPATH% (Microsoft Windows NT/2000/XP). To modify the CLASSPATH, use the same procedure you used for the PATH variable. Class path wildcards allow you to include an entire directory of .jar files in the class path without explicitly naming them individually.
🌐
GeeksforGeeks
geeksforgeeks.org › java › classpath-in-java
CLASSPATH in Java - GeeksforGeeks
October 6, 2021 - Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. All the classes listed here are imported or if we want to import a specific one then do use it as stated below. ... The JVM knows where to find the class Menu. Now, how will the JVM know this location? It is impractical for it to go through every folder on your system and search for it. Thus, using the CLASSPATH variable we provide it the place where we want it to look.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › understanding java’s classpath vs. build path
Understanding Java’s Classpath vs. Build Path | Baeldung
August 28, 2024 - While both classpath and build ... purposes. The classpath is an environment variable used by the Java Virtual Machine (JVM) to locate and load classes when running a Java program....
🌐
Lenovo
lenovo.com › home
Java Classpath: Definition, Usage, Options & Best Practices | Lenovo US
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 ...
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath.html
Setting the class path
Classpath entries that are not either a directory or an archive (.zip or .jar file) are ignored. The class path tells Java tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform.
🌐
Wikipedia
en.wikipedia.org › wiki › Classpath
Classpath - Wikipedia
May 30, 2026 - Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when the class is first used). The classpath tells Java where to look in the filesystem for files defining these classes.
🌐
Princeton
cs.princeton.edu › courses › archive › fall97 › cs461 › jdkdocs › tooldocs › win32 › classpath.html
classpath - Environment Variables
The CLASSPATH environment variable tells the Java Virtual Machine and other Java applications (for example, the Java tools located in the jdk1.1.x\bin directory) where to find the class libraries, including user-defined class libraries. The CLASSPATH environment variable is set with the set command.
🌐
Coderanch
coderanch.com › t › 643884 › java › classpath-java-Head-Java-Edition
What exactly is a classpath in java? Head First Java 2nd Edition (Beginning Java forum at Coderanch)
John Drulo wrote:1) What a class path is exactly? The "classpath" is an environment variable that programs can use. It basically consists of a bunch of directories. The programs then search each of those directories when looking for stuff - and as the name implies, Java uses it to find class files.
🌐
Quora
quora.com › What-is-a-class-path-in-Java-Why-is-it-needed-when-running-a-project-but-not-when-creating-it
What is a class path in Java? Why is it needed when running a project, but not when creating it? - Quora
The classpath in Java is conceptually similar to the PATH in Linux. When you run a Java application, you need to tell the runtime where on the file system to find all .class files needed to run the application.
🌐
TestMu AI
community.testmuai.com › ask a question
What is a classpath in Java, and how do I set it? - Ask a Question - TestMu AI (formerly LambdaTest) Community
March 11, 2025 - I came across this line: “The first thing the format() method does is load a Velocity template from the classpath named output.vm.” What does classpath mean in this context, and how do I properly set the Java classpath to ensure that my application can locate the necessary resources?
🌐
UCSB
sites.cs.ucsb.edu › ~cappello › 50-2005-Winter › lectures › classpath.html
Setting the class path
The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either: the -classpath option when calling an SDK tool (the preferred method) ...
🌐
DEV Community
dev.to › martingaston › understanding-the-java-classpath-building-a-project-manually-3c3l
Understanding the Java Classpath: Building a Project Manually - DEV Community
July 27, 2019 - Which is just charming. Essentially, our classpath needs to contain the path to our .jar files to the top of our package hierarchies. It can be set either via environment variable, which you shouldn't do, or with the much better option of the -cp flag. It also helps explain a common Java gotcha, where you try and run java without cd'ing into the directory first.
🌐
Medium
medium.com › @rostyslav.ivankiv › what-is-java-classpath-what-every-developer-should-know-e5f648bde862
What is Java Classpath? What every developer should know | by Rostyslav Ivankiv | Medium
March 5, 2023 - To simplify, the Java Classpath is just a collection of paths (directories and JAR files) used by the Java Compiler to compile and by Java Virtual Machine (JVM) to look for classes or other resources that are required by a Java program at runtime.
🌐
IBM
ibm.com › docs › en › i › 7.5.0
Java classpath
The Java virtual machine uses the Java classpath to find classes during runtime. Java commands and tools also use the classpath to locate classes. The default system classpath, the CLASSPATH environment variable, and the classpath command parameter all determine what directories are searched when looking for a particular class.
🌐
O'Reilly
oreilly.com › library › view › learning-java-4th › 9781449372477 › ch03s04.html
The Classpath - Learning Java, 4th Edition [Book]
June 25, 2013 - In a Unix shell, the PATH environment ... command. The Java CLASSPATH environment variable, similarly, is a list of locations that are searched for Java class files....
Authors   Patrick NiemeyerDaniel Leuck
Published   2013
Pages   1007
🌐
Google Groups
groups.google.com › g › play-framework › c › H17U-tTmw0U
[2.0.4-java] Getting classpath at runtime
In prod, it should work with no problems. In dev however, it's much more complex, the Play classloader is a series of URL classloaders, so to get the *entire* classpath you may need to walk up the Classloader tree using ClassLoader.getParent.
🌐
Sololearn
sololearn.com › en › Discuss › 292227 › what-is-different-between-classpath-and-path-in-java-environment-variable-
what is different between classpath and path in java ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.