10

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

Izzo
  • 4,461
  • 13
  • 45
  • 82

1 Answers1

12

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.

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56
  • So what will be returned from 'this.getClass().getResourceAsStream("/TestFileFolder/TRANSFER.xls");'? I'm confused as to what this will give me and where I will put that. – Izzo Dec 08 '13 at 08:51
  • Never mind. I realized what you showed would replace the file input stream. This seemed to solve my problem. – Izzo Dec 08 '13 at 09:01
  • 2
    Then why don't you accept the answer by clicking on the checkbox? – Lajos Arpad Dec 08 '13 at 09:56