Try the next:

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.csv");

If the above doesn't work, various projects have been added the following class: ClassLoaderUtil1 (code here).2

Here are some examples of how that class is used:

src\main\java\com\company\test\YourCallingClass.java
src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java
src\main\resources\test.csv
// java.net.URL
URL url = ClassLoaderUtil.getResource("test.csv", YourCallingClass.class);
Path path = Paths.get(url.toURI());
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
// java.io.InputStream
InputStream inputStream = ClassLoaderUtil.getResourceAsStream("test.csv", YourCallingClass.class);
InputStreamReader streamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader);
for (String line; (line = reader.readLine()) != null;) {
    // Process line
}

Notes

  1. See it in The Wayback Machine.
  2. Also in GitHub.
Answer from Paul Vargas on Stack Overflow
🌐
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.
Discussions

Access all files in a jar resources without knowing directory
The approach I'd prefer in this case would be to pass in all of this information as command line parameters. If the number of command line parameters grows too unwieldy, they can always be specified in a command line argument file ( since Java 9 ?). One thing to pay attention to when processing inputs from a user is to make sure your program parses user input early in the process and errors out immediately if the user provides nonsense. A good way to ensure this is to translate all user input that's passed as an argument into something that's strongly typed and only allows valid input. For example, if there's a command line option that has three valid inputs, then you should declare an enum to represent that input and map whatever value is passed in on the command line to one of those enum values. This ensures that you don't get deep into your program and try to process something that was passed in and run into a brick wall because it wasn't valid. Similarly, if a user passes in the path of a config file, immediately read it in and parse and validate all of the content before getting into the guts of your program. Make sure the path is clear right at program startup. Get all of the config, put it in strongly typed, immutable form, and use that to start up your program. More on reddit.com
🌐 r/javahelp
3
1
December 4, 2024
"getResourceAsStream" can't find "resource file"
uhm resources should be located on the classpath. It's not clear if this is the case in your setup, probably not. Is there any particular reason why you're using "resources" instead of reading the json as a regular file? Resources are usually meant to be included into Jars to be distributed with your code. More on reddit.com
🌐 r/javahelp
4
1
April 27, 2021
How to Properly Create Resources Directory Using Netbeans 11

It's not a problem with Netbeans.

resources default location in Maven is src/main/resources. That is correct according to your image.

try { 
 String path= this.getClass().getClassLoader().ge  tResource("bern.png").toString(); 
} catch (IOException ex) { System.out.println(path); } 

This is not compilable code...

path should be declared outside the try to use it in the catch.

Paste your entire code to pastebin or similar please

java.lang.NullPointerException
 at rvby1.lab4.circuitmaker.MainFrame.<init>(MainFrame.java:66)

This tells me nothing without you entire code.

More on reddit.com
🌐 r/javahelp
6
5
October 11, 2019
Kotlin + Gradle + Junit: how to locate test resource files?
Wait, aren't you creating Kotlin classes for your JUnit tests? The point of using better tools isn't to make your life more difficult. More on reddit.com
🌐 r/Kotlin
3
1
October 8, 2018
🌐
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. A resource may be found in a different entry in the CLASSPATH than the location where the class file was loaded. The implementation of getResource on a class loader depends on the details of the ClassLoader class.
🌐
Baeldung
baeldung.com › home › java › java io › get a path to a resource in a java jar file
Get a Path to a Resource in a Java JAR File | Baeldung
February 8, 2025 - This means it can locate resources regardless of their location within the project structure, making it more flexible for accessing resources located outside the class’s package. Starting from Java 7, we can use the Paths.get() method to obtain ...
🌐
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 - Learn to read a file from the resources folder in a Java application. We will learn to read the file present inside the jar file, and outside the Jar file as well.
🌐
Coderanch
coderanch.com › t › 757738 › java › resources-folder
How to get resources folder (Beginning Java forum at Coderanch)
A JAR is a ZIP file with some extra resources inside it. A Web Application is a WAR, which is a JAR with even more extra resources specific to webapps in it. And so forth. There are other Java archive formats as well, but JARs and WARs are the best-known. And this is why you cannot "get" the resources folder.
🌐
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 - Path class can be considered an upgrade of the java.io.File with some additional operations in place. The following code shows how to read a small file using the new Files class: @Test public void whenReadSmallFileJava7_thenCorrect() throws IOException { String expected_value = "Hello, world!"; Path path = Paths.get("src/test/resources/fileTest.txt"); String read = Files.readAllLines(path).get(0); assertEquals(expected_value, read); }
Find elsewhere
🌐
Davidvlijmincx
davidvlijmincx.com › home › java › read a file from resources directory
Read a file from resources directory
February 14, 2024 - The file you want to read is specified inside the getResourceAsStream() method as a parameter. To read a text file with an InputStream you need to create a BufferedReader that can read the lines from the file. In this quick post, I showed you two common ways to read a file from a resource directory ...
🌐
Kodejava
kodejava.org › how-do-i-load-file-from-resources-directory
How do I load file from resource directory? - Learn Java by Examples
July 2, 2023 - ClassLoader classLoader = getClass().getClassLoader(); URL resource = classLoader.getResource("database.conf"); properties.load(new FileReader(Objects.requireNonNull(resource).getFile())); System.out.println("JDBC URL: " + properties.get("jdbc.url")); InputStream is = classLoader.getResourceAsStream("database.conf"); properties.load(is); System.out.println("JDBC URL: " + properties.get("jdbc.url")); } Below is the main program that calls the methods above. package org.kodejava.lang; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Objects; import java.util.Properties; public class LoadResourceFile { public static void main(String[] args) throws Exception { LoadResourceFile demo = new LoadResourceFile(); demo.loadUsingClassMethod(); demo.loadUsingClassLoaderMethod(); } }
🌐
GitHub
gist.github.com › gkhays › 1d90f75d9347cc2d7bc70f7bd56f31e7
How to load resources and files in Java · GitHub
ClassLoader loader = ListResources.class.getClassLoader(); System.out.printf("Resource: %s\n", loader.getResource("org/gkh/ListResources.class")); Resource: file:/C:/Users/haysg/Development/WorkBench/Snippets/Snippets/bin/org/gkh/ListResources.class · Whereas the system property java.class.path shows what would be passed with the -cp or -classpath switch.
🌐
GeeksforGeeks
geeksforgeeks.org › advance java › read-file-from-resources-folder-in-spring-boot
Read file from Resources Folder in Spring Boot - GeeksforGeeks
November 3, 2025 - Java · import org.springframework.core.io.ClassPathResource; InputStream inputStream = new ClassPathResource("File1.txt").getInputStream(); This approach is useful when you do not need to inject ResourceLoader and prefer directly accessing classpath resources.
🌐
Coderanch
coderanch.com › t › 734998 › java › Resources-jar-file-practices
Resources in a jar file - best practices? (Java in General forum at Coderanch)
September 26, 2020 - I also have an important directory structure under the resources folder and use the methods in File to find all files under that directory. I guess this isn't something that can be done with an input stream - so is this type of use case just unsupported then? I can always just find a different way, I guess there's no reason I can't just put the resources folder in the root directory. ... Hmmm... there's a class java.util.JarFile which looks like it might support that kind of thing.
🌐
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 - ApplicationContext context; public Resource loadEmployeesWithApplicationContext() { return context.getResource("classpath:data/employees.dat"); } 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"); }
🌐
Oracle
docs.oracle.com › javase › tutorial › deployment › webstart › retrievingResources.html
Retrieving Resources (The Java™ Tutorials > Deployment > Java Web Start)
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. Use the getResource method to read resources from a JAR file.
🌐
Java By Examples
javabyexamples.com › access-files-as-resources-using-spring
Home | Java By Examples
In this example, we're declaring ResourceLoader as a dependency, and Spring autowires it for us. Moreover, we don't need to define a ResourceLoader bean, since the ApplicationContext bean is also a ResourceLoader. In the greet method, we're calling the ResourceLoader.getResource method with the ...
🌐
Novixys Software
novixys.com › blog › read-file-resources-folder-java
How to Read a File from Resources Folder in Java | Novixys Software Dev Blog
January 10, 2017 - You can access these files and folders from your java code as shown below. The following code snippet shows how to load resources packed thus into the jar or war file: String respath = "/poems/Frost.txt"; InputStream in = sample2.class.getResourceAsStream(respath); if ( in == null ) throw new Exception("resource not found: " + respath);
🌐
Xenovation
xenovation.com › home › blog › java › java - read file from classpath
Java - Read file from classpath | XENOVATION
April 24, 2019 - 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. package com.xenovation; import java.io.File; import java.net.URL; public class ClasspathExample { public File readFileFromClasspath() { URL fileUrl = getClass().getResource("/example.xml"); return new File(fileUrl.getFile()); } }
🌐
Uofr
uofr.net › ~greg › java › get-resource-listing.html
Java: Listing the content of a resource directory - UofR.net
The ClassLoader.getResource() function can be a really handy way to load up your files in Java. The files can be loaded from any folder or JAR file on your classpath. However, the API disappointingly lacks a way to list all the files in the directory.
🌐
Medium
medium.com › @rasheedamir › java-how-to-load-a-file-resources-folder-dcded6b5c089
Java — How to load a file from resources folder? | by Rasheed Amir | Medium
August 26, 2016 - Sometime during tests you may want to read a file from resources folder; and here is the java code which you can use to read the file: ClassLoader classloader = Thread.currentThread().getContextClassLoader();// read as input stream InputStream is = classloader.getResourceAsStream("GameOfThronesS01E01.m3u8");// read as a file File file = new File(classLoader.getResource("GameOfThronesS01E01.m3u8").getFile());
🌐
Medium
medium.com › swlh › a-convenient-way-to-load-and-parse-resources-in-java-4061681f68bd
A Convenient Way to Read Resources in Java | by Alexei Klenin | The Startup | Medium
September 7, 2020 - One day, after writing code to load and parse JSON for 100500th time I understood one thing: reading content of resources in Java is harder than it should be! Just take a look. Let’s assume we have a resource com/adelean/junit/jupiter/resource.txt containing text: “The quick brown fox jumps over the lazy dog.” ... Looks scary, doesn’t it? Need to open and close multiple streams/readers, handle IOException, all just to get text content. This Stackoverflow topic (https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder), and many others, explain how to load resources.