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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › windows › classpath.html
2 Setting the Class Path
April 21, 2026 - When classes are stored in an archive file (a zip or JAR file) the class path entry is the path to and including the zip or JAR file. For example, the command to use a class library that is in a JAR file as follows: java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool
Discussions

java - What is a classpath and how do I set it? - Stack Overflow
Please explain what was meant by classpath in this context, and how I should set the 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: More on stackoverflow.com
🌐 stackoverflow.com
How to read text file from classpath in Java? - Stack Overflow
I am trying to read a text file which is set in CLASSPATH system variable. Not a user variable. I am trying to get input stream to the file as below: Place the directory of file (D:\myDir) in CLASS... More on stackoverflow.com
🌐 stackoverflow.com
Trying to understand classpath file
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 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. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar 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: 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/javahelp
9
2
March 29, 2024
Understanding Java classpath.
A .jar file is really just a zipfile with a bunch of classes inside, these classes are generally inside packages (namespaces) that match a directory structure. So a jar is really just a directory tree with classes. The classpath is just a complete directory tree of classes that gets formed from the standard library (java.util.List, etc.), the librarary jar files (CoolFramework.jar for example), and your own source code. This 'classpath' is really just an in-memory directory tree. So if you use org.javaguy.coolframework.SomeClass in your code, the JVM knows to look in ./org/javaguy/coolframework for a SomeClass.class file and load that. More on reddit.com
🌐 r/learnprogramming
4
2
January 13, 2021
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 ...
🌐
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 - 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.
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.

🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › 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 a JDK tool (the preferred method) or by setting ...
Top answer
1 of 16
678

With the directory on the classpath, from a class loaded by the same classloader, you should be able to use either of:

// From ClassLoader, all paths are "absolute" already - there's no context
// from which they could be relative. Therefore you don't need a leading slash.
InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream("SomeTextFile.txt");
// From Class, the path is relative to the package of the class unless
// you include a leading slash, so if you don't want to use the current
// package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");

If those aren't working, that suggests something else is wrong.

So for example, take this code:

package dummy;

import java.io.*;

public class Test
{
    public static void main(String[] args)
    {
        InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt");
        System.out.println(stream != null);
        stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt");
        System.out.println(stream != null);
    }
}

And this directory structure:

code
    dummy
          Test.class
txt
    SomeTextFile.txt

And then (using the Unix path separator as I'm on a Linux box):

java -classpath code:txt dummy.Test

Results:

true
true
2 of 16
137

When using the Spring Framework (either as a collection of utilities or container - you do not need to use the latter functionality) you can easily use the Resource abstraction.

Resource resource = new ClassPathResource("com/example/Foo.class");

Through the Resource interface you can access the resource as InputStream, URL, URI or File. Changing the resource type to e.g. a file system resource is a simple matter of changing the instance.

Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-classpath-in-java
How to Set Classpath in Java? - GeeksforGeeks
July 23, 2025 - The CLASSPATH variable is used to specify the location of the Java class files and libraries required for the program.
🌐
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.
🌐
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
🌐
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 ...
🌐
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 - The classpath is an environment variable used by the Java Virtual Machine (JVM) to locate and load classes when running a Java program. It specifies a list of directories, JAR files, and ZIP files where the JVM should look to find and load class ...
🌐
Reddit
reddit.com › r/javahelp › trying to understand classpath file
r/javahelp on Reddit: Trying to understand classpath file
March 29, 2024 -

I just started learning Java so we have a study group where we are supposed to create a simple desktop application using Eclipse IDE and WindowBuilder.

I created the base project and then proceeded to push it to the shared repository. The rest of the people in the team tried executing the base project without success, while I could run it without any issues.

After a while, one of the members realized there is a problem with the classpatch file:

<?xml version="1.0" encoding="UTF-8"?>

<classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE\_CONTAINER"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/com.ibm.icu\_74.2.0.jar" sourcepath="/snap/eclipse/85/plugins/com.ibm.icu\_74.2.0.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/jakarta.annotation-api\_2.1.1.jar" sourcepath="/snap/eclipse/85/plugins/jakarta.annotation-api\_2.1.1.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.core.commands\_3.12.0.v20240214-1640.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.core.commands\_3.12.0.v20240214-1640.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.core.runtime\_3.31.0.v20240215-1631.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.core.runtime\_3.31.0.v20240215-1631.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.e4.ui.di\_1.5.300.v20240116-1723.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.e4.ui.di\_1.5.300.v20240116-1723.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.equinox.common\_3.19.0.v20240214-0846.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.equinox.common\_3.19.0.v20240214-0846.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.equinox.registry\_3.12.0.v20240213-1057.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.equinox.registry\_3.12.0.v20240213-1057.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.jface\_3.33.0.v20240214-1640.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.jface\_3.33.0.v20240214-1640.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.jface.text\_3.25.0.v20240207-1054.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.jface.text\_3.25.0.v20240207-1054.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.osgi\_3.19.0.v20240213-1246.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.osgi\_3.19.0.v20240213-1246.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.swt.gtk.linux.x86\_64\_3.125.0.v20240227-1638.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.swt.gtk.linux.x86\_64\_3.125.0.v20240227-1638.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.text\_3.14.0.v20240207-1054.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.text\_3.14.0.v20240207-1054.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.ui.forms\_3.13.200.v20240108-1539.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.ui.forms\_3.13.200.v20240108-1539.jar"/> <classpathentry kind="lib" path="/snap/eclipse/85/plugins/org.eclipse.ui.workbench\_3.131.100.v20240221-2107.jar" sourcepath="/snap/eclipse/85/plugins/org.eclipse.ui.workbench\_3.131.100.v20240221-2107.jar"/> <classpathentry kind="output" path="bin"/> </classpath>

This is making reference to several libs which exist only in my notebok, where I am using Ubuntu. They are trying to run it in Windows.

Is it not kind of stupid to refer to these libs this way? I am still trying to understanc why Java would make reference to these libs assuming all of us would be using Ubuntu? Nobody else in my team has the snap folder :/

Could you please help me understand what is going on? How can we fix it?

I tried looking for some videos explaining the classpath file but no luck so far.

Thank you :(

Top answer
1 of 4
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 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. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar 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: 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.
2 of 4
1
The Eclipse .classpath and .project files as you have found out have absolute file system refs pointing to dependent jars, plugins and other resources that only work on your machine, unless all your team mates have PCs set up with identical filesystem layouts and files in expected locations … highly unlikely. For this reason .classpath, .project, .settings are usually ignored from your git commits and not shared with others. As an alternative, use a Maven pom.xml file to describe your 3rd party dependencies. Each other dev can import the project as a Maven project into Eclipse and it will download deps and set your classpath. Gradle is another alternative.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-set-classpath-when-class-files-are-in-jar-file-in-java
How to Set Classpath When Class Files are in .jar file in Java? - GeeksforGeeks
July 23, 2025 - 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.
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath.html
Setting the class path
To find class files in the directory /java/MyClasses as well as classes in /java/OtherClasses, you would set the class path to: % java -classpath /java/MyClasses:/java/OtherClasses ...
🌐
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.
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › classpath.html
Setting the Classpath in Java
To use our standard library files ... same library files, scattered in different directories. Java's classpath mechanism allows you to store and access the library files from a common directory....
🌐
Blogger
javarevisited.blogspot.com › 2011 › 01 › how-classpath-work-in-java.html
How to Set Classpath for Java on Windows and Linux? Steps and Example
Classpath in Java is the path to the directory or list of the directory which is used by ClassLoaders to find and load classes in the Java program. Classpath can be specified using CLASSPATH environment variable which is case insensitive, -cp ...
🌐
Reddit
reddit.com › r/learnprogramming › understanding java classpath.
r/learnprogramming on Reddit: Understanding Java classpath.
January 13, 2021 -

I was surfing stackoverflow, trying to better understand what the classpath is all about and came upon this question/answer. It mostly makes sense, but there are two slightly confusing paragraphs in the accepted answer that go:

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.

I don't understand the two sentences that say:

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.

Why would the VM "look inside the jar file for the org/javaguy/coolframework part"? Why does that structure exist within the jar file? The answer mentions that you bundle "CoolFramework up into a .jar file", so why would that include the org/javaguy/coolframework hierarchy? Isn't CoolFrameWork just a single directory?

🌐
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 - The classpath is crucial for Java applications, as it informs the Java Virtual Machine (JVM) where to find user-defined and third-party classes. The classpath can include the input for directories, JAR files (Java ARchive - a package file format typically used to aggregate many Java class files and associated metadata and resources), and ZIP files where Java classes and resources are located.
🌐
How to do in Java
howtodoinjava.com › home › i/o › reading a file from classpath in java
Reading a File from Classpath in Java
April 11, 2022 - To read any file from the classpath in a class, we have to get the reference of the system classloader for that class that is trying to read the file. System classloader obviously knows the other paths for the application.