Maven:尝试使用Intellij或在jar中使用Java命令从资源中打开文件时,java.io.FileNotFoundException [英] maven: java.io.FileNotFoundException when trying to open file from resources with Intellij or using java command with jar

查看:105
本文介绍了Maven:尝试使用Intellij或在jar中使用Java命令从资源中打开文件时,java.io.FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从intellij的"Java 8 In Action"一书中导入了

然后,我从Intellij直接从ExecuteAround类执行Main方法(右键单击->执行main ...)

 公共静态void main(String ... args)引发IOException {//我们要重构的方法以使其更加灵活字符串结果= processFileLimited();System.out.println(结果);System.out.println(-");字符串oneLine = processFile((BufferedReader b)-> b.readLine());System.out.println(oneLine);字符串twoLines = processFile((BufferedReader b)-> b.readLine()+ b.readLine());System.out.println(twoLines);}公共静态字符串processFileLimited()引发IOException {尝试(BufferedReader br =new BufferedReader(new FileReader("lambdasinaction/chap3/data.txt"))){返回br.readLine();}}公共静态字符串processFile(BufferedReaderProcessor p)引发IOException {尝试(BufferedReader br =新的BufferedReader(新的FileReader("lambdasinaction/chap3/data.txt"))){返回p.process(br);}}公共接口BufferedReaderProcessor {public String process(BufferedReader b)抛出IOException;} 

然后,我得到了FileNotFoundException:

 线程主"中的异常java.io.FileNotFoundException:lambdasinaction \ chap3 \ data.txt 

如果执行一个"Maven程序包",则在jar中,data.txt文件直接包含在chap3文件夹/程序包中,但是如果执行,则会出现相同的错误:

  java -classpath lambdasinaction-1.0.jar lambdasinaction.chap3.executeAround线程主"中的异常java.io.FileNotFoundException:lambdasinaction \ chap3 \ data.txt(可解释)在java.io.FileInputStream.open0(本地方法)在java.io.FileInputStream.open(未知来源)在java.io.FileInputStream上.< init>(未知来源)在java.io.FileInputStream上.< init>(未知来源)位于java.io.FileReader.< init>(未知来源)在lambdasinaction.chap3.ExecuteAround.processFileLimited(ExecuteAround.java:23)在lambdasinaction.chap3.ExecuteAround.main(ExecuteAround.java:9) 

1-为什么它不能直接从intellij运行?该文件正确位于资源文件夹中.

2-这可能是最重要的:为什么直接从命令行执行程序时为什么不运行?

感谢您的帮助

解决方案

在执行Java程序时,相对于当前工作目录的相对文件名如 lambdasinaction/chap3/data.txt 会被解析:

  • 使用Intellij默认设置启动程序时,当前目录是包含 .idea 文件夹的目录(还包含 pom.xml *.iml *.ipr 等)
  • 使用命令行时,当前目录可以在Linux上使用 pwd 打印,而在Windows上使用 cd 打印出来

文件放置在 src/main/resources 下,因此,除非当前目录是 resources ,否则您始终会收到 FileNotFoundException 's.部署后,您的程序很可能找不到您的资源.

好消息是该目录对于Maven和与Maven兼容的工具(例如Intellij)来说是特殊的,因为其内容可作为类路径资源使用-这意味着您将始终能够使用以下命令读取这些文件

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

注意:我不是使用文件系统(FileReader)来解析文件名,而是使用(绝对)类路径资源标识符.

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

解决方案

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.

这篇关于Maven:尝试使用Intellij或在jar中使用Java命令从资源中打开文件时,java.io.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆