简体   繁体   English

导出为.jar文件后的FileNotFoundException

[英]FileNotFoundException after exporting as .jar file

I'm using Eclipse and developing a java application where I use of a local file. 我正在使用Eclipse并开发一个使用本地文件的java应用程序。 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. 该文件位于项目文件夹内,在子文件夹中,说“mydata / myfile.txt”当我从eclipse运行应用程序时一切正常,我打开一个FileInputStream,传递“mydata / myfile.txt”作为名称,文件是读得很好。 But when I export my application as .jar, and try to run it, all I get is a FileNotFoundException in the console. 但是当我将我的应用程序导出为.jar并尝试运行它时,我得到的只是控制台中的FileNotFoundException。

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. 先感谢您。

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. 当您的应用程序导出为jar时,您的配置文件不能作为文件提供,因为它包含在.jar文件中。 Instead you should access it via ClassLoader.getResourceAsStream() . 相反,您应该通过ClassLoader.getResourceAsStream()访问它。 See this SO question for more background info. 有关更多背景信息,请参阅此SO问题

也许这会对你有所帮助

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

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

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