kneedhelp kneedhelp - 4 years ago 255
Java Question

jar file not working because of image

I think my executable jar file is not working because the image I'm using is not being loaded. When I made a batch file and ran the jar it said

"IllegalArgumentException: input == null!"


The program works perfectly when I run it in eclipse. So my question is do you need to do something special with images in order to run a jar file correctly?

Code I think is causing the error:

BufferedImage image = ImageIO.read(Sprite.class.getResource(path));
this.width = image.getWidth();
this.height = image.getHeight();
this.pixels = new int[width * height];
image.getRGB(x, y, width, height, pixels, 0, width);


Edit: the program works fine (images load) in eclipse, it's just when I export it as an executable jar file that the program doesn't work.

Answer Source

If anyone wants to know how I solved it:

I changed the

Sprite.class.getResource(path)

to

getClass().getResourceAsStream(path)

and I moved the images to the same location as the jar. For example, I had both the jar (exported it to the desktop) and the images on the desktop, and the path was "/" plus the file name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download