You don't need to mess with class loaders. In fact it's a bad habit to get into because class loader resources are not java.io.File objects when they are in a jar archive.

Maven automatically sets the current working directory before running tests, so you can just use:

    File resourcesDirectory = new File("src/test/resources");

resourcesDirectory.getAbsolutePath() will return the correct value if that is what you really need.

I recommend creating a src/test/data directory if you want your tests to access data via the file system. This makes it clear what you're doing.

Answer from Steve C 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 - URL resource = getClass().getClassLoader().getResource("file.txt"); if (resource == null) { throw new IllegalArgumentException("file not found!"); } else { // failed if files have whitespaces or special characters //return new File(resource.getFile()); return new File(resource.toURI()); } // for static access // URL resource = JavaClassName.class.getClassLoader().getResource("fileName"); 1.1 Review the files in the src/main/resources, later we will access the files and print out the file content.
Discussions

How to get src/main/resources path from Java - Stack Overflow
I have some stuff under src/main/resources path. Specifically I have a folder with report templates called reports. I understand that when the application is deployed/run all files and folders und... More on stackoverflow.com
🌐 stackoverflow.com
maven - Java path to file in src/main/resources - Stack Overflow
I have a maven module with the following directory structure and I want to get the path to abc.access in abcManager.java. src |-main |-java |-org.abc.xyz |-init |-abcManager.java |-resources |-abc.access More on stackoverflow.com
🌐 stackoverflow.com
java - Read file from /src/main/resources/ - Stack Overflow
String relativeWebPath ...Context().getRealPath(relativeWebPath); File f = new File(absoluteDiskPath); ... As you can see on the image I am trying to access words.txt but it isn't working. Any ideas? ... /src/main/resources/, though present in your project directory structure, is probably not a part of the Web path... More on stackoverflow.com
🌐 stackoverflow.com
java - How to get the path to a file in src/main/resources folder? - Stack Overflow
Assume standard maven setup. I have a file abc.conf in src/main/resources folder of my project. How do i get the absolute path to abc.conf in the code? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › testing › get the path of the /src/test/resources directory in junit
Get the Path of the /src/test/resources Directory in JUnit | Baeldung
February 20, 2026 - This is because the ClassLoader looks for the resources on the classpath. In Maven, the compiled classes and resources are put in the /target/ directory. That’s why this time, we got a path to a classpath resource. In this brief article, we discussed how to read a /src/test/resources directory in JUnit 5.
🌐
Coderanch
coderanch.com › t › 757738 › java › resources-folder
How to get resources folder (Beginning Java forum at Coderanch)
File operations will not work on resources inside JARs, no matter what path you use. You have to use classloader resource methods. Now that gives me an error. I am using this code: I know I am wrapping a String inside a String, but I will leave it. And here is how my directories look like: ... Did you read my post at all? I carefully describe that resource folders contain packages, and the name of a resource is based on the name of the package that it is in. So if src/main/resources is a resource folder, then org.ckdpixels.ckdpixels.shaders is the name of the package that your resource is in, and therefore the absolute resource name is "/org/ckdpixels/ckdpixels/shaders/defaultV.glsl".
Find elsewhere
🌐
Makble
makble.com › what-is-the-srcmainresources-folder-for-in-java-project
What is the src/main/resources folder for in Java project - Java - Makble
It's in bin folder because we are executing the code through Run As - Java Application which is using the Eclipse built-in build tool. The bin folder is where Eclipse build tool stores compiled classes and resources. To run it using Gradle, add this line to build.gradle ... File f = new File(ClassLoader.getSystemClassLoader().getResource("logback.xml").getFile()); System.out.println(new String(Files.readAllBytes(f.toPath())));
🌐
amitph
amitph.com › home › java › read a file from resources folder in java
Read a File from Resources Folder in Java - amitph
November 22, 2024 - Example of using the newBufferedReader() method from Java NIO Files class to read a file from the resources directory. try ( BufferedReader br = Files.newBufferedReader( Paths.get("src/main/resources/file.txt")); Stream<String> lines = br.lines(); ) { lines.forEach(System.out::println); }Code language: Java (java)
🌐
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. A file outside the jar file may be present as a war file or an Eclipse project in the development environment. 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“.
🌐
Vaadin
vaadin.com › forum › t › how-to-get-path-to-directory-in-resources › 159508
How to get path to directory in resources - Vaadin Forum
October 26, 2019 - Hi Vaadin:) I am working with Vaadin 14 with Spring boot and I have problem with trying to reach directory with sounds in resources as a File class. File soundsDir = new File(SOUNDS_DIRECTORY); // creating file with …
🌐
Sololearn
sololearn.com › en › Discuss › 1028852 › how-to-get-srcmainresources-from-junit
How to get /src/main/resources from Junit?
January 25, 2018 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
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 ... package. Starting from Java 7, we can use the Paths.get() method to obtain a Path object representing a resource within a JAR file....
🌐
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.
🌐
Medium
medium.com › codex › accessing-resource-files-in-spring-boot-a-comprehensive-guide-eab958eb8f9c
Accessing Resource Files in Spring Boot: A Comprehensive Guide | by Igor Venturelli | CodeX | Medium
November 28, 2024 - Resource files in a Spring Boot application are files bundled within the application, usually located in the src/main/resources directory. These files can be anything from static files (like images, HTML, CSS) to configuration files (such as application.properties, YAML), or even templates that are rendered in the browser. When packaged into a JAR, these resource files are placed under the resources folder inside the JAR file. To access these resources, you need to reference them using paths relative to the classpath, meaning their location is relative to where they reside in the JAR or the project.