Jakub Ferjak Jakub Ferjak - 1 year ago 61
Java Question

Resources folder not working when exported

I've got this strange problem with Java/Eclipse. I use this to load BufferedImages (and a similar one to load Fonts):

ClassLoader classLoader = ResourceLoader.class.getClassLoader();
ImageIO.read(new File(classLoader.getResource(path).getFile()));

This code is in a class called ResourceLoader. It is an utility class that I made and use to load BufferedImages and Fonts. Everything works in Eclipse, but when exported to executable JAR, it throws a nullPointerException when trying to load the resources. I looked into the JAR and the resources are packed in there.

The path is in the following format:

Here is the hierarchy of my project:

My project hierarchy

Any ideas why it isn't working?

Answer Source

The contents of jars are not files and folders, so you can not access their internals as if they were. The classloader can give you a direct InputStream for the resource. ImageIO can take an InputStream. Use them together.

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