3

I have a folder called "resources" in my Eclipse project. It's used as source folder. Everything works well compiled from Eclipse. I wanted to try it on Ubuntu so I exported it to runnable jar and tried to run but it gave me FileNotFound Exception. I checked the file structure inside the JAR and there's no "resources" folder, only files that should be inside. I'm accessing files with

File file = new File("resources/hotkeys.ini");

I also tried

InputStream in = getClass().getClassLoader().getResourceAsStream("/resources/hotkeys.ini");

but it gave me null.

How to force Eclipse to export the folder along with jar?

ssedano
  • 8,322
  • 9
  • 60
  • 98
ahoj pepiczki
  • 309
  • 1
  • 3
  • 18

2 Answers2

1

When you use "Export" functionality, you need to select all resources that need to be exported (it is visible at the top of the Eclipse export wizard window).

You can find the answer here as well: Java: export to an .jar file in eclipse

Community
  • 1
  • 1
Michal Borek
  • 4,584
  • 2
  • 30
  • 40
1

You can open the jar with 7zip or Winzip, and check the path.

Try

getClass().getResourceAsStream("/resources/hotkeys.ini");

(Or try removing the first / when with getClassLoader().)

Especially check the case-sensitivity of the paths. As opposed to Windows, inside a Jar it is case-sensitive.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • After unpacking there's no "resources" folder, just .inis. Also your code did nothing new. – ahoj pepiczki May 03 '13 at 21:54
  • 1
    `getResourceAsStream("/hotkeys.ini")`? – Joop Eggen May 03 '13 at 21:56
  • So `hotkeys.ini` is in the top directory of the jar, and the spelling is not `Hotkeys.INI` but is all small letters? – Joop Eggen May 03 '13 at 22:47
  • Yes, it's "hotkeys.ini", no uppercase letters. – ahoj pepiczki May 04 '13 at 06:53
  • I think I'll use `file = new File(System.getProperty("user.dir")+"/hotkeys.ini");` and put .inis outside the JAR. – ahoj pepiczki May 04 '13 at 08:01
  • `getResource` and `getResourceAsStream` should work, or when not finding return `null`. More likely something else is fishy. Do you read with `new InputStreamReader(inputStream, "Windows-1252")`, that is explicitly stating the encooding. I would first do some debugging to the error point. – Joop Eggen May 04 '13 at 11:32