I am creating a runnable/executable jar for a program that uses some image files on disk. When I hard-code the explicit location of the files, the jar executes just fine. But since the end users won't have the image files (and even if they did, they would not be located in the same place as my code points to), I want to make a relative reference to the location, and include them in the jar.
Here is the code I am using:
No matter where I place the image files, they do not end up in the jar, and therefore the jar crashes with the following exception:
Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(Unknown Source)
There is no place in Eclipse to "Add Files" to the jar.
My class files are located at: C:\JavaCode\Game\bin\com\russ
I tried putting the image files in each of those sub-directories and they never end up in the jar.
Why aren't the image files being included in the jar? What am I doing wrong? Any help is appreciated.
Okay, here's the answer.
Eclipse needs the resource files to reside in two places. One place so that the IDE can use them, and in another place so that they can be included in the jar. Placing them in just one of these locations will not take care of the other problem. They need to reside in both places.
The two places are:
1) The root of the bin directory (NOT inside of a sub-directory there)
2) In a sub-directory under the project folder.
Eclipse has an easy way to add the sub-directory for location #2. Right click the project and select New > Source Folder.
As long as the files live in both places, the IDE can use them AND they will be included in the jar. Thank you to Ralph Cook and Wouter Oet for helping me narrow down this problem.
Joined: Oct 13, 2005
Well done sorting it out, and thank you for providing the answer.