Java JAR can't find file

ぃ、小莉子 提交于 2019-11-27 04:39:24

问题


I am running a program in eclipse that references a file in the program's source folder. However, when I export the program into a runnable JAR, the program cannot seem to find the file. Essentially, the program works perfect in eclipse but doesn't do so when it's a standalone program.

I've attached a photo of how I reference the file in the program.


回答1:


Once it's in the jar, you can't find it using new File() -- it's now a class path resource. You need to use this.getClass().getResourceAsStream("/TestFileFolder/TRANSFER.xls"); (or, if your method is static, you need to use the className.class in place of getClass()).

this is actually a feature -- if for any reason you need to change the TRANSFER.xls resource, you can do so by shadowing it in the classpath without repacking the jar.



来源:https://stackoverflow.com/questions/20451386/java-jar-cant-find-file

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