Image excluded when exporting into a jar?

My java project looks like this:
FirstGame
src
(default package)
0.png

(The code is stored in default package.)

I load my images:

public static void loadImages() {
		for (byte i = 0; i < 1; i++) {
			try {
				image[i] = ImageIO.read(new File("src/" + i + ".png"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

(No errors are given when compiling and I can display the image in the game.)

When I export the project into a runnable jar file it seems as if the image doesn’t exist. I can extract the jar’s contents with 7-zip and I can clearly see my image is there however.

So the question is, how do I make sure the image is included and in the right place when I export it?

Replace your code with this:


public static void loadImages() {
		for (byte i = 0; i < 1; i++) {
			try {
				image[i] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("/src/" + i + ".png"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

Why your code doesnt work: You cant access a file inside a JAR file with File. You need the input stream.

I replaced the code and got an error message regarding static context. After editing the function to non-static in your version of the code I got this error upon compiling:

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
	at javax.imageio.ImageIO.read(Unknown Source)
	at Background.loadImages(Background.java:16)
	at Game.main(Game.java:45)

Background.java:16:

image[i] = ImageIO.read(getClass().getClassLoader()
						.getResourceAsStream(i + ".png"));

Game.java:45:

new Background().loadImages();

The last piece of code is executed when launching the project. I normally would use the entire class Background in a static context and I suspect that might be the problem.
Does anybody have any idea?

Seems way too simple and trivial but did you try removing the “/” before “src”?

image[i] = ImageIO.read(Foo.class.getClassLoader().getResourceAsStream("/src/" + i + ".png"));

to

image[i] = ImageIO.read(Foo.class.getClassLoader().getResourceAsStream("src/" + i + ".png"));

I already tried that with no luck :-\

Your problem is your class is in the source folder already, so you don’t need to reference it when loading the image. Try removing the “src/” completely when loading the image.

I edited my code like your suggestion yesterday without trying it out because I was tired and thought it was going to do nothing.
I just tried it out and everything works perfectly! Thanks for the help everyone!

Edit: Follow-up question out of curiosity: What use is there in using my original method of grabbing images since it doesn’t work after exporting?

Your former method doesn’t work when inside JAR because [icode]File[/icode] isn’t supported inside JAR files. It can be useful if the resources were placed outside the JAR file.

So I can export a jar file and have my folder of images in the same directory, and the jar could grab those files?

Yes for sure.

Thanks a bunch! Going back to actually making progress on my game!