I have an image inside my jar which I am trying to load, but getResourceAsStream() always returns null.
Directory structure:
com/thesimpzone/watch_pugs/watch_pugs/{all my class files}
META-INF/MANIFEST.MF
resources/generic/mouse.png
Content.java:
public abstract class Content {
protected Map<String, BufferedImage> images = new HashMap<String, BufferedImage>();
protected String prefix;
public Content(String prefix){
this.prefix = prefix;
}
protected void loadImage(String name){
System.out.println(name);
System.out.println(prefix);
String path = (prefix + name);
System.out.println(path);
String identifier = name.substring(0, name.lastIndexOf("."));
try{
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
images.put(identifier, ImageIO.read(in));
}catch(IOException | ClassCastException e){
throw new RuntimeException("Image " + identifier + " at " + path + " could not be loaded.");
}
}
[...]
}
GenericContent.java:
public class GenericContent extends Content {
public GenericContent(){
super("resources/generic/");
this.loadContent();
}
@Override
public void loadContent() {
loadImage("mouse.png");
}
}
StackTrace:
mouse.png
resources/generic/
resources/generic/mouse.png
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at com.thesimpzone.watch_pugs.watch_pugs.content.Content.loadImage(Content.java:29)
at com.thesimpzone.watch_pugs.watch_pugs.content.GenericContent.loadContent(GenericContent.java:17)
at com.thesimpzone.watch_pugs.watch_pugs.content.GenericContent.<init>(GenericContent.java:12)
at com.thesimpzone.watch_pugs.watch_pugs.Canvas.<init>(Canvas.java:45)
at com.thesimpzone.watch_pugs.watch_pugs.Framework.<init>(Framework.java:75)
at com.thesimpzone.watch_pugs.watch_pugs.Window.<init>(Window.java:50)
at com.thesimpzone.watch_pugs.watch_pugs.Window.<init>(Window.java:26)
at com.thesimpzone.watch_pugs.watch_pugs.Window$1.run(Window.java:60)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I have no idea why the classloader can't find the image. I have looked in the compiled jar and the file is there, and opens fine in paint so the file is OK. I have tried various ClassLoader variations, including getSystemClassLoader(), getClassLoader(), and Content.class.getClassLoader(); and also getResourceAsStream(path) instead of getResource(path).openStream(). I have tried with and without the leading '/' on the prefix, so I'm all out of ideas and Google isn't helping. Also, it seems like what I'm doing to define 'prefix' is very awkward and so if there is a better way I would be glad if someone showed me how.
Thanks.