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.
🌐
Baeldung
baeldung.com › home › spring › access a file from the classpath in a spring application
Access a File from the Classpath using Spring | Baeldung
March 26, 2025 - Note that from Resource, we can easily jump to Java standard representations like InputStream or File. Another thing to note here is that the above method works only for absolute paths. If we want to specify a relative path, we can pass a second class argument. The path will be relative to this class: new ClassPathResource("../../../data/employees.dat", Example.class).getFile();
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
But 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, to use a class library that is in a .jar file, the command would look something like this: C:> java -classpath C:\java\MyClasses\myclasses.jar ...
🌐
GitHub
gist.github.com › 573 › 3939250
example .classpath file for eclipse · GitHub
January 10, 2022 - example .classpath file for eclipse. GitHub Gist: instantly share code, notes, and snippets.
🌐
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
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.

🌐
Wikipedia
en.wikipedia.org › wiki › Classpath
Classpath - Wikipedia
May 30, 2026 - Being closely associated with the file system, the command-line Classpath syntax depends on the operating system. For example:
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › deployment › jar › downman.html
Adding Classes to the JAR File's Classpath (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
The following is an example of the Class-Path manifest attribute: Class-Path: servlet.jar infobus.jar acme/beans.jar images/ By using the Class-Path attribute in your application's JAR file manifest, you can avoid having to specify a long -classpath flag when launching java to run your application.
🌐
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 - Learn to read a file from classpath in Java. The file can be present at root of class path location or in any relative sub-directory.
🌐
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.
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.

🌐
UCSB
sites.cs.ucsb.edu › ~cappello › 50-2005-Winter › lectures › classpath.html
Setting the class path
For example, to use a class library that is in a .jar file, the command would look something like this: % java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
🌐
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 - We need to add both the JFiglet and JUnit .jar files to our classpath, and now we've also got to feed in each test file and the file its testing to the compiler.
🌐
GeeksforGeeks
geeksforgeeks.org › java › different-ways-to-set-a-classpath-in-java
Different Ways to Set a Classpath in Java - GeeksforGeeks
July 23, 2025 - The limitation of -cp,-classpath, ... F:\workspace\src // It's class file is located in F:\workspace\bin class GFG { public static void main(String[] args) { System.out.println("GFG!"); } }...
🌐
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 - To set the classpath via the command line, we use the -classpath option when running the java command: ... Here, MyProgram is the name of the main class, and /path/to/class/files is the directory where the class file is located.
🌐
Study.com
study.com › business courses › java programming tutorial & training
What is Classpath in Java? - Definition & Example - Lesson | Study.com
March 20, 2019 - We can set the value of CLASSPATH in DOS. The following example changes the variable to a local folder that we've created called CustomClasses; it's located in a folder on the C: drive called Java: If you have your classes saved in a zip file, you can use the same command as above, except add ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › loading-resources-from-classpath-in-java-with-example
Loading Resources from Classpath in Java with Example - GeeksforGeeks
July 23, 2025 - In the below code, we are considering file named GFG_text.txt which acts as a resource, and we are taking care that this resource is in same path as that of our .class file. Code 1: Using getResourceAsStream() method. ... // Java program to load resources from Classpath // using getResourceAsStream() method.
🌐
IBM
ibm.com › docs › en › spm › 8.0.2
The .classpath file
Use this information to understand how the .classpath file maintains the project's source and target references for Java compilation and compressed file or project dependencies
🌐
GeeksforGeeks
geeksforgeeks.org › java › read-a-file-from-the-classpath-in-java
How to Read a File From the Classpath in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Read a File from the Classpath import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; // Driver Class public class MyClass { // Main Function public static void main(String[] args) { // Filename String filePath = "MyFile.txt"; // Loading the current class's class loader for inputStream InputStream inputStream = MyClass.class.getClassLoader().getResourceAsStream(filePath); // Checking if the class loaded successfully if (inputStream != null) { try (BufferedReader reader = new BufferedReader(new InputStreamReade
🌐
Xenovation
xenovation.com › home › blog › java › java - read file from classpath
Java - Read file from classpath | XENOVATION
April 24, 2019 - Now we can read resource file in Java. Lets say that we have created Java project called ClasspathExample and we need to read example.xml from it, firstly we need to add resource folder to classpath and then read it.