简体   繁体   中英

Can't find resources runnable jar

I can't load an resource file after extracting a project as a runnable jar.

It works in Eclipse but after exporting it throws a FileNotFoundException .

I have tried to put the res folder next to the .jar file but nothing helps. I've tried with JarSplice and got it running with all the libraries but it stops with the resource file. 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.

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. For an example, see this question on how to determine the current working directory from inside 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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