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.
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