0

I have spent all last night (until 3am) and this morning researching, testing, refactoring, and attempting to debug this issue. I have a simple Java game in Netbeans and while it runs perfectly perfect within the IDE in either run or debug mode, once exported into a jar file it refuses to load any resources corrrectly. There are many similar questions to this such as this one regarding loading an ImageIcon and despite great effort none of these solutions work for my project. I am not using ImageIcons, only simple BufferedImages and wav sound files. I recently refactored to combine my BufferedImageLoader and Sound classes into one Resource class, which I then moved into the same package as all my resources even though it worked perfectly well in a separate code package before in the IDE, although it works in its new location as well, strictly within the IDE.

I'm rather irritated and flustered from this issue. The truly infuriating thing is that this project used to work with resources after being exported into a jar, and now it seems to have stopped working with no changes. The only real programmatic difference between back when it worked and now is that I didn't have or use sound files back then, but this error isn't related to the sound files, as it catches an exception (and generates an error dialog) just from first trying to load the art assets.

I've tried every possible solution I've found in my research to no avail. Hopefully a fresh set of eyes can reveal the error of my ways.

The offending line of code is

return ImageIO.read(Resource.class.getResource("/res/" + imageFileName));

whereas imageFileName is the parameter with values passed from method calls such as

blockSheet = Resource.loadImage("art_assets/platform.png");

The location of the Resource class seemed to have no bearing on this working within Netbeans. My res folder is inside src, next to the com class package beginning.

It throws an IllegalArgumentException: input == null! exception. After some testing it seems that Resource.class.getResource("/res/" + imageFileName) returns a null value, which makes no sense at all. Again, this works perfectly perfect within the IDE. I can change the jar file into a zip and look inside to see that all the resources are exactly where they should be with the correct names and the correct extensions.

Here is a zip file of my entire project. Any help is immensely appreciated. Thank you.

EDIT:

Some of the things I've already tried:

getResourceAsStream() instead of getResource()

classLoader() between Resource.class and getResource()

this.getClass() instead of Resource.class from a non-static context

Community
  • 1
  • 1
Gunnar Clovis
  • 35
  • 1
  • 10

1 Answers1

0

I think this should help: How to get the path of a running JAR file?

CodeSource codeSource = YourMainClass.class.getProtectionDomain().getCodeSource(); 
File jarFile = new File(codeSource.getLocation().toURI().getPath());
String jarDir = jarFile.getParentFile().getPath();

provided by Benny Neugebauer in the post.

Community
  • 1
  • 1
Aimert
  • 146
  • 6