I am trying to load a resource in a jar, here is the exported jar:
'main' is the package with all my classes, and in one of those classes I am trying to load the background.png file. In my Eclipse project I put the resources under a "res/" folder, which I added to the build path to include it. When I try to use
new File("background.png");
It can't find the file. When I use
MyClass.class.getClass().getClassLoader().getResource("background.png");
It still can't find the file.
Files packaged in a jar can't be accessed as
File
objects.When you try
you are actually using the
ClassLoader
ofjava.lang.Class
and not ofmain.MyClass
which may not be able to find the resource (in case it is the system classloader). Tryinstead.