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
- See it in The Wayback Machine.
- Also in GitHub.
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
- See it in The Wayback Machine.
- Also in GitHub.
Try:
InputStream is = MyTest.class.getResourceAsStream("/test.csv");
IIRC getResourceAsStream() by default is relative to the class's package.
As @Terran noted, don't forget to add the / at the starting of the filename
java - How to get resources directory path programmatically - Stack Overflow
How do I add a resources folder to my Java project in Eclipse - Stack Overflow
file - Get folder from Resources folder JAVA - Stack Overflow
Get the path from a resource folder, only the path
Videos
I'm assuming the contents of src/main/resources/ is copied to WEB-INF/classes/ inside your .war at build time. If that is the case you can just do (substituting real values for the classname and the path being loaded).
URL sqlScriptUrl = MyServletContextListener.class
.getClassLoader().getResource("sql/script.sql");
Finally, this is what I did:
private File getFileFromURL() {
URL url = this.getClass().getClassLoader().getResource("/sql");
File file = null;
try {
file = new File(url.toURI());
} catch (URISyntaxException e) {
file = new File(url.getPath());
} finally {
return file;
}
}
...
File folder = getFileFromURL();
File[] listOfFiles = folder.listFiles();
When at the "Add resource folder",
Build Path -> Configure Build Path -> Source (Tab) -> Add Folder -> Create new Folder

add "my-resource.txt" file inside the new folder. Then in your code:
InputStream res =
Main.class.getResourceAsStream("/my-resource.txt");
BufferedReader reader =
new BufferedReader(new InputStreamReader(res));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
To answer your question posted in the title of this topic...
Step 1--> Right Click on Java Project, Select the option "Properties"
Step 2--> Select "Java Build Path" from the left side menu, make sure you are on "Source" tab, click "Add Folder"

Step 3--> Click the option "Create New Folder..." available at the bottom of the window
Step 4--> Enter the name of the new folder as "resources" and then click "Finish"

Step 5--> Now you'll be able to see the newly created folder "resources" under your java project, Click "Ok", again Click "Ok"

Final Step --> Now you should be able to see the new folder "resources" under your java project

It won't work if resources/images_resultats is not in your classpath and/or if it is in a jar file.
Your code should rather be something like this:
File[] file = (new File(getClass().getResource("/my/path").toURI()))
.listFiles();
You can determine what files are in a folder in resources (even if its in a jar) using the FileSystem class.
public static void doSomethingWithResourcesFolder(String inResourcesPath) throws URISyntaxException {
URI uri = ResourcesFolderUts.class.getResource(inResourcesPath).toURI();
try( FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap() ) ){
Path folderRootPath = fileSystem.getPath(inResourcesPath);
Stream<Path> walk = Files.walk(folderRootPath, 1);
walk.forEach(childFileOrFolder -> {
//do something with the childFileOrFolder
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
inResourcesPath should be something like "/images_resultats"
Note that the childFileOrFolder paths can only be used while the FileSystem remains open, if you try to (for example) return the paths then use them later you've get a file system closed exception.
Change ResourcesFolderUts for one of your own classes
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!
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.