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
Answer from Jon Skeet on Stack Overflow
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.

🌐
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 - As a caveat, there is another way to retrieve resources in Spring, but the ResourceUtils Javadoc is clear that the class is mainly for internal use. ... public File loadEmployeesWithSpringInternalClass() throws FileNotFoundException { return ResourceUtils.getFile( "classpath:data/employees.dat"); } We should carefully consider the rationale, as it’s probably better to use one of the standard approaches above. Once we have a Resource, it’s easy for us to read the contents.
🌐
Mkyong
mkyong.com › home › java › java – read a file from resources folder
Java - Read a file from resources folder - Mkyong.com
September 4, 2020 - In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath.
🌐
Xenovation
xenovation.com › home › blog › java › java - read file from classpath
Java - Read file from classpath | XENOVATION
April 24, 2019 - If the file that we want to be read is in a jar file, then we need to add jar file to our Classpath · Next (just to be sure) we are going to double check the build directory. To do that go here: ProjectName > Properties > Java Build Path > Source tab · Now we can read resource file in Java.
🌐
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.
🌐
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
🌐
Steranka Blog
steranka.com › 2024 › 10 › 25 › java-read-file-from-classpath.html
Java - Read file from classpath | Steranka Blog
October 25, 2024 - Important TIPS when reading files on classpath You can read a resource using multiple approaches (not all work the same). AClass.class.getClassLoader().getResourceAsStream(file); // preferred ClassLoader.getSystemResourceAsStream(resourcePath) AClass.class.getResourceAsStream() Depending on h~~~~ow the code is written you either NEED to use a leading slash in the name of the resource loaded or don’t need to use one.
🌐
Delft Stack
delftstack.com › home › howto › java › java read file from classpath
How to Read File From Classpath in Java | Delft Stack
February 14, 2024 - Exception handling is integrated into a try-catch block, addressing potential IOExceptions for file reading issues and guarding against a null URL with a NullPointerException if the resource is not found. This method ensures a straightforward and robust approach to reading files from the classpath in Java.
🌐
TutorialsPoint
tutorialspoint.com › loading-resources-from-classpath-in-java-with-example
Loading Resources from classpath in Java with Example
July 28, 2023 - In summary, there exist diverse methods of loading resources from classpath in Java-such as utilizing ClassLoader.getResource() or ClassLoader.getResourceAsStream(). Subsequently, determining which mode to apply is contingent on individual usage criteria and respective requisites of your app. Once you have secured an InputStream or URL towards the resource file- you can apply varying tactics when reading its contents; using a BufferedReader serves for text files.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › loading-resources-from-classpath-in-java-with-example
Loading Resources from Classpath in Java with Example - GeeksforGeeks
July 23, 2025 - InputStream instr = obj.getCla... while ((line = rr.readLine()) != null) System.out.println(line); } } ... Code 2: Using getResource() method....
🌐
Baeldung
baeldung.com › home › java › java io › how to read a file in java
How to Read a File in Java | Baeldung
January 8, 2024 - This section explains how to read a file that is available on a classpath. We’ll read the “fileTest.txt” available under src/main/resources:
🌐
Blogger
javarevisited.blogspot.com › 2014 › 07 › how-to-load-resources-from-classpath-in-java-example.html
How to Load Resources from Classpath in Java with Example
If you look closely you will find that we have used both getResource() and getResourceAsStream() methods to load resources from classpath in Java, in this case just properties file.
🌐
W3Docs
w3docs.com › java
How to read text file from classpath in Java?
To read a text file from the classpath in Java, you can use the getResourceAsStream method of the ClassLoader class to get an InputStream for the file, and then use a BufferedReader to read the contents of the file.
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot 2 › read a file from resources in spring boot
Read a File from Resources in Spring Boot
August 27, 2023 - We can specify the absolute path to the resource on the file system with prefix 'file:'. ServletContextResource: Loads a resource from the ServletContext. We can specify the path to the resource relative to the ServletContext. It does not use any prefixes. @Autowired ResourceLoader resourceLoader; //Read resource Resource classPathResource = resourceLoader.getResource("classpath:file.txt"); Resource urlResource = resourceLoader.getResource("http://example.com/file.txt"); Resource fileSystemResource = resourceLoader.getResource("file:/path/to/file.txt"); Resource servletContextResource = resourceLoader.getResource("/WEB-INF/file.txt"); //Get file content File file = resource.getFile());
🌐
javathinking
javathinking.com › blog › how-to-read-a-file-from-jar-in-java
How to Read a File from a JAR in Java: Step-by-Step Classpath Resource Guide — javathinking.com
Use MyApp.class.getClassLoader().getResourceAsStream("resource-path"). The resource path is relative to the classpath root (no leading slash). import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import ...
🌐
How to do in Java
howtodoinjava.com › home › i/o › read a file from resources directory
Read a File from Resources Directory - HowToDoInJava
December 8, 2022 - When packaging the application as jar file, the file present in the '/resources' folder are copied into the root 'target/classes' folder. In this case, the file location is inside a zipped archive like jar-filename.jar/!filename.txt. We should directly read this file as InputStream.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › guides › lang › resources.html
Location-Independent Access to Resources
April 21, 2026 - The ClassLoader methods search each directory, ZIP file, or JAR file entry in the CLASSPATH for the resource file, and, if found, returns either an InputStream, or the resource name. If not found, the methods return null.
🌐
Bryanbende
bryanbende.com › development › 2014 › 10 › 20 › extracting-classpath-resources
Extracting Classpath Resources
October 20, 2014 - Now once you have this list of resources you can use getResourceAsStream() to read each resource and write the contents somewhere: Collection<String> resources = ResourceList.getResources(pattern); for (String resource : resources) { InputStream in = null; try { in = this.getClass().getCla...