0

I'm been stuck for a while trying to load images. I've been looking everywhere, but most "solutions" such as .getResource, .getResourceAsStream have not been working for me.

Recently I tried looking at the path that the .jar is pulling the file from, and it seems to be working properly

I got the .jar to print to console the path that it was following using the code

// (path is \Resources\(image).png)
System.out.println(new File (ImageLoader.class.getProtectionDomain().getCodeSource()
   .getLocation().toURI().getPath() + path));  

giving me something like C:\path\Desktop\game.jar\Resources\background.png

I opened my .jar file using an archiver and it gave me the same path. C:\path\Desktop\game.jar\Resources\ and all my images are there.

Loading from my IDE works properly, but not from the .jar. (the issue shouldn't have to do with case sensitivity). when I actually try to load it using

ImageIO.read(new File(ImageLoader.class...), it doesn't work (returns null);

Does this problem have to do with the .jar not being to access the files inside itself because of permission issues or something else? Thanks!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Mike
  • 1
  • 1
  • "Files" don't exist within Jar files. Resources do. – Hovercraft Full Of Eels May 07 '16 at 17:37
  • Hi Eels, Oh okay, I see what you mean. However, I've had some issues using .getResource and .getResourceAsStream. I tried one of the solutions provided by the response, and I currently have URL url = new URL("jar:file:\\" + ImageLoader.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath() + "!" + path); InputStream is = url.openStream(); image = ImageIO.read(is); but it doesn't seem to work. (null pointer) – Mike May 07 '16 at 17:56

0 Answers0