Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JAR can't find file

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.

enter image description here

like image 244
Izzo Avatar asked Dec 08 '13 08:12

Izzo


1 Answers

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.

like image 171
PaulProgrammer Avatar answered Oct 09 '22 15:10

PaulProgrammer