简体   繁体   中英

FileInputStream Javafx in .jar files

I am creating a Javafx application in Intelij and FileInputStream works perfectly. However, when I create a .jar file from the project and try to run it the code fails to run as it is unable to locate the file in the file input stream. Here is my code:

ObjectInputStream os = new ObjectInputStream(new FileInputStream("src/settingStorage.bin"));

Am I doing something wrong?

There is always some issue accessing a file outside a jar, depending on where the file location is. You can check this SO question/answer to have an idea on accessing your file.

Read properties file outside JAR file

Try this:

EDIT: Look at this again, I left part out.

ObjectInputStream os = new ObjectInputStream(new FileInputStream(SomeClass.class.getResourceAsStream(“settingStorage.bin")));

This usually works for me. When I run this, it operates fine, in AND out of the development area. Don't include /src, as when you call getResourceAsStream off a class, it already checks inside the jar.

Cheers!

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