'Exporting resource folder with jar

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?



Solution 1:[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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Joop Eggen