FileNotFoundException in src/main/resources
Asked Answered
S

4

23

i placed a file in my maven project under src/main/resources the files name is simply temp.txt.

When i try to open the file:

BufferedReader br = new BufferedReader(new FileReader(new File("./temp.txt")));

I get an error:

Exception in thread "main" java.io.FileNotFoundException: \temp.txt

all files under src/main/resources are placed in the root folder of the classpath under maven. So why cant the program find the file here?

Sackey answered 8/5, 2014 at 16:32 Comment(3)
do you need the '.'?Landan
that changes nothing. The '.' just says the current directory. Without '.' is the same like with the '.'.Sackey
try removing it anywaysLandan
I
31

If you're going to package the file in the class path, then read it as such.. from the class path.

Maven Structure

src
   main
       resources
               file.txt

After it builds, the file gets placed in the root of the class path. So use

InputStream is = getClass().getResourceAsStream("/file.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));

The / in front of file.txt will bring you to the root, from whatever package the class is in.


UPDATE

Test example

package com.underdogdevs.stackoverflow;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestResourceFile {

    public static void main(String[] args) throws IOException {
        InputStream is = TestResourceFile.class.getResourceAsStream("/test.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

enter image description here

enter image description here

Isopleth answered 8/5, 2014 at 16:43 Comment(5)
And your file is in src/main/resources/file.txt?Isopleth
Review your build and verify that the file is copied correctly. Is not there some typo?Peal
With the / = null. Without the / = NullPointerException?Goodin
Gotta say, the statement is dope "If you're going to package the file in the class path, then read it as such.. from the class path."Besmirch
Sorry for now ask this. But If i wanna get more resources. What can I do? because when I can't get classpath or anything when running jar.Frigidarium
A
6

just give this as your path:

BufferedReader br = new BufferedReader(new FileReader(new 
File("src/main/resources/temp.txt")));
Adigranth answered 4/5, 2018 at 10:11 Comment(1)
This is not going to work after you export your project. You will no longer have access to the source code (src dir) when the project is deployed. If you are placing files into the resource dir, then they should be read as classpath resources.Isopleth
H
2

Had this problem when setting up integration tests in Jenkins.
The issue was caused by having the job inside a folder with spaces in the name. So instead of having a workspace folder named foo bar, Jenkins created foo%20bar and the tests all failed with FileNotFoundException.

The solution is just to rename your folder so it doesn't have any spaces.

Hardfavored answered 6/11, 2018 at 10:47 Comment(0)
B
0

Maven puts the files under /src/main/resouces/ into the default package of your classpath. Hence you can load through the classloader:

InputStream in = getClass().getResourcesAsStream("temp.txt")

For more information see Class#getResoucesAsStream.

Bonspiel answered 8/5, 2014 at 16:43 Comment(1)
same exception. and all other files i load on the same way and it works perfectly.Sackey

© 2022 - 2024 — McMap. All rights reserved.