简体   繁体   English

找不到资源可运行的jar

[英]Can't find resources runnable jar

I can't load an resource file after extracting a project as a runnable jar. 将项目提取为可运行的jar后,我无法加载资源文件。

It works in Eclipse but after exporting it throws a FileNotFoundException . 它可以在Eclipse中运行,但是在导出后会抛出FileNotFoundException

I have tried to put the res folder next to the .jar file but nothing helps. 我试图将res文件夹放在.jar文件旁边,但没有任何帮助。 I've tried with JarSplice and got it running with all the libraries but it stops with the resource file. 我已经尝试过JarSplice,并使它与所有库一起运行,但它随资源文件一起停止。 The object file is located in a source folder. 目标文件位于源文件夹中。

What can I do? 我能做什么?

Code

    FileReader fr = null;
    try {
        fr = new FileReader(new File("res/" + fileName + ".obj"));
    } catch (FileNotFoundException e) {
        System.err.println("Couldn't load object file!");
        e.printStackTrace();
    }

EDIT: By opening the runnable .jar file in 7zip I can see that the whole /res folder has disappeared during the exporting and the files in the directory now lies directly in the root folder of the .jar file. 编辑:通过在7zip中打开可运行的.jar文件,我可以看到整个/ res文件夹在导出过程中已经消失,目录中的文件现在直接位于.jar文件的根文件夹中。

Based on your code, the res folder should be placed directly off the present working directory , which should be the directory where you are running the Java command from. 根据您的代码, res文件夹应直接放在当前工作目录下 ,该目录应该是您从中运行Java命令的目录。 For an example, see this question on how to determine the current working directory from inside Java . 有关示例,请参见有关如何从Java内部确定当前工作目录的问题

Use Path instead of new File(string). 使用路径而不是新的文件(字符串)。

FileReader fr = null;
try {
    fr = new FileReader(Paths.get("res/" + fileName + ".obj").toFile());
} catch (FileNotFoundException e) {
    System.err.println("Couldn't load object file!");
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM