I can't seem to find the correct combination of path and class vs. ClassLoader. Here is the directory structure of my project:
The source for the ImageLoader is as follows:
public class ImageLoader {
public ImageLoader(){...}
public BufferedImage loadImage(String fileName) {
String loc = "resources" + File.separator + "images" + File.separator;
URL imgURL = this.getClass().getResource(loc + fileName);
BufferedImage img = null;
try {
img = ImageIO.read(imgURL);
} catch (IOException e) {...}
return img;
}
}
I'm not entirely sure what the difference would be between that and this.getClass().getClassLoader().getResource()
, but I have tried it with various combinations of paths and I simply can't seem to get the .jar to find and load the resources.
Where am I going wrong?
Thanks.
Don't use File.separator
for getting at a resource, it should always be '/'. It might pay to also add a leading '/' to indicate that the resource can be found from the 'top' of the class-path structure (as opposed to a sub-directory of the class that is trying to load it).