First off, I would like to address that this question is very similar to the one here:getResources() returns an empty Enumeration . I chose to ask this because mine is more of a question of JAR files, as well as because the former question has yet to be answered. In addition, this is technically a different method entirely (getResource(String) instead of getResources()). Now that that is out of the way...
I am getting a mysterious error message from my code when it is in a Runnable JAR file, yet this doesn't happen at all when I am testing the project in Eclipse. The error message is:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.wierd0games.Under.graphics.Sprite.<clinit>(Sprite.java:13)
at com.wierd0games.Under.main.Main.<init>(Main.java:53)
at com.wierd0games.Under.main.Main.main(Main.java:57)
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1388)
at com.wierd0games.Under.graphics.SpriteSheet.load(SpriteSheet.java:27)
at com.wierd0games.Under.graphics.SpriteSheet.<init>(SpriteSheet.java:22)
at com.wierd0games.Under.graphics.SpriteSheet.<clinit>(SpriteSheet.java:13)
... 3 more
From my interpretation of this error, the problem is in my Sprite class at line 13. Here, I create my first Sprite with:
public static Sprite frog=new Sprite(0, 0, 64, SpriteSheet.frog);
Then the second part of the error means that the real problem is in the SpriteSheet class, which is called on by the initialization of frog, the Sprite object. The only time I use ImageIO.read is in the function load() {} which is:
private void load() {
try {
BufferedImage image=ImageIO.read(SpriteSheet.class.getResource(path));
int w=image.getWidth();
int h=image.getHeight();
image.getRGB(0, 0, w, h, pixels, 0, w);
}
catch (IOException e) {
e.printStackTrace();
}
}
... in which path is a String that is "/textures/spritesheetFrog.png"
, because the Sprite Sheet is located in a folder named textures, and is named spritesheetFrog.png.
So if the error is saying that the input is null, am I correct in that the function getReasource(String) is returning null for some reason? Does anyone have any idea why this what is happening, why is it only happening in a Runnable JAR, and most importantly, how to fix it?
EDIT: Here is a hierarchy of my classes:
src
com.wierd0games.under.graphics (this is a package)
Sprite
SpriteSheet
res
textures
spritesheetFrog.png
So if SpriteSheet is my calling class, why does /textures/spritesheetFrog.png not work? The first slash should make it go back to the beginning, then it should go to spritesheetFrog.png, right? If not, can someone explain why this doesn't work and what it should be?
EDIT: Just to avoid confusion, the res folder is NOT located inside the source, it is separate, but included in the build path.
EDIT: Okay, I changed my hierarchy in Eclipse so that textures was indeed under source. I thought that I would update it here as well, because I have yet to get an answer that has helped me solve that problem.
src
com.wierd0games.under.graphics (this is a package)
Sprite
SpriteSheet
textures
spritesheetFrog.png