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
🌐
Coderanch
coderanch.com › t › 757738 › java › resources-folder
How to get resources folder (Beginning Java forum at Coderanch)
December 29, 2022 - In this layout, src/ and res/ are so called "source folders" that contain Java source files and resource files respectively. bin/ contains the compiled class files and lib/ contains libraries that the application depends on.
Discussions

java - How to get resources directory path programmatically - Stack Overflow
I have the following directory layout: src main java resources sql (scripts for database) spring (configuration) webapp Within a ServletContextListener clas... More on stackoverflow.com
🌐 stackoverflow.com
How do I add a resources folder to my Java project in Eclipse - Stack Overflow
I want to have a place to store my image files to use in my Java project (a really simple class that just loads an image onto a panel). I have looked everywhere and cannot find how to do this. How ... More on stackoverflow.com
🌐 stackoverflow.com
file - Get folder from Resources folder JAVA - Stack Overflow
It won't work if resources/images_resultats is not in your classpath and/or if it is in a jar file. ... Sign up to request clarification or add additional context in comments. ... so I must have a folder containing my three folder images_resultats etc... More on stackoverflow.com
🌐 stackoverflow.com
Get the path from a resource folder, only the path
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://imgur.com/a/fgoFFis ) 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
4
3
November 23, 2021
🌐
Mkyong
mkyong.com › home › java › java – read a file from resources folder
Java - Read a file from resources folder - Mkyong.com
September 4, 2020 - This article will show you how to read a file from a `resources` folder, `getResourceAsStream` or `getResource`.
🌐
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 - The resources folder belongs to the Maven project structure where we place the configuration and data files related to the application. The location of the folder can be “src/main/resources” and “src/test/resources“.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 4408088791186-What-is-the-path-to-files-in-root-resources-folder
What is the path to files in root resources folder – IDEs Support (IntelliJ Platform) | JetBrains
October 12, 2021 - Once you have marked a folder as resource folder in IntelliJ, it is available in your code as via resource loader. ... I think I'm more confused now, haha. Maybe my root folder is in the wrong place? Should it be inside the package that the project is in? Again, I was trying to interpret directions that were written using Eclipse, so this part might not be applicable. ... Phobos Project >.idea >Lib >out >res >>data >>>data.xlsx >>graphics >>>sewers.png >src >>com.technetium.phobosproject >>>data >>>>java class files >>>engine >>>>java class files >>>graphics >>>>java class files
🌐
Bloomu
javabook.bloomu.edu › addresources.html
How to Add Resources to an IntelliJ Project
To do this, open the File > Project Structure dialog, select the Modules tab, and click on the resources folder.
Find elsewhere
🌐
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 - Resources within a JAR file are typically accessed using a path relative to the root of the JAR file in Java.
🌐
Reddit
reddit.com › r/javahelp › get the path from a resource folder, only the path
r/javahelp on Reddit: Get the path from a resource folder, only the path
November 23, 2021 -

Hi there, I have some files in a ressource fioder which I want to access depending on a certain situation. I am trying to build a path to them like: String path = Paths.get("module-name","dir","resources").toString(); Then I build it in: String actualPath = path + separator + fileName;

Even if I put the files in another dir(not in resources) this still not works.

I get file not found exception, system can't find the path. What am I doing wrong?

Thank you!

Top answer
1 of 2
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://imgur.com/a/fgoFFis ) 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 2
1
What is your folder structure like? By 'situation' what do you mean? I really enjoy URI access to files here is an example utility class I wrote for accessing files either when running from IDE or when running from JLink/JPackage/Jar distribution. https://github.com/dionthorn/LifeSimRPG/blob/main/src/main/java/org/dionthorn/lifesimrpg/FileOpUtil.java Show us your code and folder structure.
🌐
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 - Contents1. Introduction2. Packaging Resources3. Loading the Resources4. Using Absolute Path of Resource5. Loading from Relative PathsConclusionSee Also 1. Introduction When you build a java project and pack it into a jar (or a war), the files under the resources folder are included into the jar.
🌐
MrPowerGamerBR
mrpowergamerbr.com › en › blog › 2022-08-20-listing-all-files-from-resources-working-via-cli-and-ide
Listing all files from Java Resources folder, working via the IDE and via executing the JAR on the command line • MrPowerGamerBR
August 20, 2022 - Read the resources content when running the JAR via the command line · So here's an example (Kotlin, but it should be easy to migrate it to Java) that allows you to have both: Reading the resources content when running from a IDE or via the command line!
🌐
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.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360000655999-Creating-or-jumping-to-a-resource-folder-for-a-given-class
Creating or jumping to a resource folder for a given class – IDEs Support (IntelliJ Platform) | JetBrains
September 5, 2018 - I often want to create a resource file for a class that should be in the same folder structure as the file. For example I have ... If that folder doesn't exist yet I copy the relative path of the java file, remove everything before "src", navigate to the resources folder, create a new folder and paste the path.
🌐
Google Groups
groups.google.com › g › bazel-discuss › c › YPwULx-7tS4
Resource file location
Ideally I'd like it to match existing Gradle behavior that treats "src/main/resources" as a root, so the properties file would appear at the root level in the jar. ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... It'll be under the java/ directory if there is one (so if your directory structure was java/src/main/... you'd get the right path) or your build root (as you discovered).
🌐
Reddit
reddit.com › r/javahelp › where to place resource folder for getresourceasstream()?
r/javahelp on Reddit: Where to place resource folder for GetResourceAsStream()?
January 27, 2019 -

I can't seem to figure this one out (I've scoured the internet, trust me). I built my path to a resource folder directly in my Project folder. It's alongside my premade src folder.

I am constantly having an IllegalArgumentException show up because my InputStream is null.

 private BufferedImage imageCreator(String uri) throws IOException {
	System.out.println(uri);
	InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(uri);
	System.out.println(inputStream);
	BufferedImage image = ImageIO.read(inputStream);
	return image;
 }

Then, my calling of the method:

 try {
     sprite = imageCreator("res/testSprite.png");
 } catch (IOException e) {
  e.printStackTrace();
 }

I have tried putting a slash behind /res, removing res completely, and adding a slash behind testSprite when it's on its own. Everytime, it gets the same error.

Edit:

Before throwing in the cards for the night, I decided to try to put the images in a package within my res folder. This worked. The images are now being put into my bin folder.

🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › guides › lang › resources.html
Location-Independent Access to Resources
March 16, 2026 - Methods in the classes Class and ClassLoader provide a location-independent way to locate resources. For example, they enable locating resources for: An applet loaded from the Internet using multiple HTTP connections. An applet loaded using JAR files. A Java Bean loaded or installed in the ...
🌐
Claytex
claytex.com › home › the “resources” folder
The "Resources" folder - Claytex
August 11, 2023 - The basic syntax, of a URI, to access a file in the Resources folder is: modelica://<Library name>/Resources/<filename> where <Library name> is the name of the library and <filename> is the path of the the file relative to the Resources folder.