How to load an image from a jar? [solved]

my code:


	public static BufferedImage grabImage(String path, int x, int y, int width, int height) {
		BufferedImage tile = null;
		BufferedImage bildTileset = null;
		try {
			bildTileset = ImageIO.read(new File(path));
		} catch (IOException error) {
			System.err.println("Tileset not found");
			error.printStackTrace();
		}
		tile = bildTileset.getSubimage(x * width, y * height, width, height);
		return tile;
	}

//exapmple 
// BufferedImage img = grabImage("res/img/img.png",0,0,16,16);

But it wont work! It loads if it is outside of the jar! How do i load it form inside the jar?

In addition to what BurntPizza posted, I’ll offer my own explanation:

Stuff inside of jars aren’t files anymore. They’re resources. So the File class will no longer work with stuff inside a jar- because they aren’t files. They’re resources.

Instead, you need to access them as resources. You do this using the handy getResource() and getResourceAsStream() methods found in ClassLoader (use this if your resources are relative to your whole jar) and Class (use this if your resources are relative to your class).

For example, your problematic line might look like this instead:

ImageIO.read(YourClassName.getClass().getClassLoader().getResourceAsStream(path));

More info here: http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html
Or in the API: http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-
Or just google “java load resources” for a ton of results.

Edit: I should also note that the getResource() methods will also work with files outside your jar. So it’s perfectly reasonable to use these methods from your IDE as well.

Indeed, although “resources” is a bit hand-wavey IMO, they are technically zip-file entries, as jar files are actually thinly veiled zip archives.

Correct. Either way, the File class doesn’t work with them, and the getResource() methods must be used instead.

I believe that one can also use a JarInputStream or JarFile to read the currently executing jar as if it was a directory structure as well, although certainly getResource() is much more convenient. Just pointing out that there’s nothing magical about the “currently executing jar,” it’s a readable file like any other.

But yeah, just use getResourceAsStream() and use Riven’s tool I linked if you have trouble.

You can use JarFile and JarInputStream (and JarEntry) to view the contents of a jar, but to view the contents of the contents I’m pretty sure you’d have to extract them to the file system outside the jar first.

In other words, you can use the Jar* classes to see that a Jar contains a .txt file, but to actually view that .txt file, you’d have to extract it first. Or just use the getResource() methods.

You could do that, or read entries piecemeal via JarFile.getInputStream() if I’m not mistaken.

There is no need to store archive entry contents to disk to read them, just like there is no need to store socket data to disk to read it. Just read bytes from the stream.