FileNotFoundException after exporting as .jar file

佐手、 提交于 2019-12-19 04:03:22

问题


I'm using Eclipse and developing a java application where I use of a local file. The file is inside the project folder, in a subfolder, say "mydata/myfile.txt" When I run the app from eclipse everything works fine, I open a FileInputStream passing "mydata/myfile.txt" as the name and the file is read just fine. But when I export my application as .jar, and try to run it, all I get is a FileNotFoundException in the console.

How can I export the program so that it can run on any computer and be able to access the file? Thank you in advance.


回答1:


When your app is exported as a jar, your config file is not available as a file, since it's wrapped up in the .jar file. Instead you should access it via ClassLoader.getResourceAsStream(). See this SO question for more background info.




回答2:


Maybe this will help you

InputStream in=ClassLoader.getSystemResourceAsStream("your file");


来源:https://stackoverflow.com/questions/10871827/filenotfoundexception-after-exporting-as-jar-file

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