maven: java.io.FileNotFoundException when trying to open file from resources with Intellij or using java command with jar

情到浓时终转凉″ 提交于 2021-01-29 03:09:01

问题


I've imported the github's project from the book "Java 8 In Action" in intellij as a maven project.

The module structure is the following:

Then, i'm executing the Main method from the ExecuteAround class directly from Intellij (right click -> execute main...)

public static void main(String ...args) throws IOException{

    // method we want to refactor to make more flexible
    String result = processFileLimited();
    System.out.println(result);

    System.out.println("---");

    String oneLine = processFile((BufferedReader b) -> b.readLine());
    System.out.println(oneLine);

    String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
    System.out.println(twoLines);

}

public static String processFileLimited() throws IOException {
    try (BufferedReader br =
                 new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))) {
        return br.readLine();
    }
}


public static String processFile(BufferedReaderProcessor p) throws IOException {
    try(BufferedReader br = new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))){
        return p.process(br);
    }

}

public interface BufferedReaderProcessor{
    public String process(BufferedReader b) throws IOException;

}

And then, i'm getting a FileNotFoundException:

Exception in thread "main" java.io.FileNotFoundException: lambdasinaction\chap3\data.txt

If a execute a "maven package", in the jar, the data.txt file is directly included in the chap3 folder/package but i get the same error if I execute:

java -classpath lambdasinaction-1.0.jar lambdasinaction.chap3.executeAround
Exception in thread "main" java.io.FileNotFoundException: lambdasinaction\chap3\data.txt (Le chemin d?accès spécifié est introuvable)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileReader.<init>(Unknown Source)
        at lambdasinaction.chap3.ExecuteAround.processFileLimited(ExecuteAround.java:23)
        at lambdasinaction.chap3.ExecuteAround.main(ExecuteAround.java:9)

1- Why is it not running directly from intellij? The file is correctly in the resource folder.

2 - This is probably most important: Why is it not running when executing the programm directly from the command line?

Thanks for the help


回答1:


When you execute a Java program, relative filenames like lambdasinaction/chap3/data.txt are resolved against the current working directory:

  • when you start your program with Intellij default settings, the current directory is the one containing the .idea folder (also contains pom.xml, *.iml, *.ipr, and so on)
  • when using the command line the current directory can be printed out with pwd on Linux and cd on Windows

The files are put under src/main/resources and so unless the current directory is that resources you'll always get FileNotFoundException's. Likely your program will not find your resources once deployed.

Good news is that directory is special for Maven and Maven-compatible tools (like Intellij) because the contents are available as classpath resources - it means you will always be able to read those files with

getClass().getResourceAsStream("/lambdasinaction/chap3/data.txt");

Note: I didn't use the filesystem (FileReader) to resolve the filename, but an (absolute) classpath resource identifier.




回答2:


Use "src/main/resources/lambdasinaction/chap3/data.txt" instead of "lambdasinaction/chap3/data.txt"



来源:https://stackoverflow.com/questions/37601586/maven-java-io-filenotfoundexception-when-trying-to-open-file-from-resources-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!