Reputation: 71
I'm having this problem where none of the resources load when I run a JAR of the program. I've changed code and checked that it is indeed both the code that loads images and the code that loads sounds do not work. It works perfectly fine when I run it in eclipse. I checked with 7-Zip and the sounds and images files aren't in a res folder anymore. I tried changing the path files and the location of the resources in my program before I exported it, but that didn't work. I also have the res folder as a source folder in the build path. I'm exporting the JAR with eclipse, and then adding my libraries into it with JarSplicer. Here are the codes that load images and sound resources:
-Load Sound-
public static WaveData LoadSound(String path){
WaveData sound = null;
try {
sound = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
} catch (Exception e) {
e.printStackTrace();
}
return sound;
}
-Load Images-
public static Texture LoadTexture(String path, String fileType){
Texture tex = null;
InputStream in = ResourceLoader.getResourceAsStream(path);
try{
tex = TextureLoader.getTexture(fileType,in);
}catch(IOException e){
e.printStackTrace();
}
return tex;
}
Here's my error in the command prompt:
Here are the files in the jar every time I exported it (regardless of whether the resources were in res or not):
I'm stumped here. If someone knows how to fix this, then please help me out.
Upvotes: 0
Views: 2641
Reputation: 21
Turns out that paths to resources are case sensitive and do not work in jar files if there is a single misstyped letter in the path.
Upvotes: 2
Reputation: 71
I found the answer to my problem.
ResourseLoader.getResourceStreamAs()
does not work inside of JAR files, so you need to do getClass().getResourceStreamAs()
, or [ClassName].class.getResourceStreamAs()
, if it's static.
Also, I had to change the location of the files from res/[resource file]/[resource]
to /[resource file]/[resource]
because when you export, it takes out all of the files in res. Also, you have to make sure that you have that /
at the beginning of the path there in order to designate it to search in the source folder, or else it will search in the folder of the class that called getResourceStreamAs()
. And also, new FileInputStream()
doesn't work in JAR files, so you have to use [ClassName].class.getResourceStreamAs()
instead. Oh, and on top of that, I had a few files that somehow got the extension part its name in all capitals for no reason. So that gave me some confusion. I basically just had to do a bunch of fiddling with code, but I got it working!
One more thing: make sure you add your resources
folder to the sources tab in the build path, or else eclipse won't export it. And if you want your libraries to be exported in your JAR, then you'll have to add them manually by making a fat jar with JarSplicer
.
Upvotes: 2