Accepted answer

when you use


getresource will try to find the resource relative to the package. if you use:


getresource will treat it as an absolute path and simply call the classloader like you would have if you'd done.


the reason you can't use a leading / in the classloader path is because all classloader paths are absolute and so / is not a valid first character in the path.


i had the same issue working on a project with maven. here how i fixed it: i just put the sources (images, musics and other stuffs) in the resources directory:


i created the same structure for the packages in the resources directory too. for example:

if my class is on


in the resources directory i put one package with the same name


so i use



one other thing to look at that solved it for me :

in an eclipse / maven project, i had java classes in src/test/java in which i was using the this.getclass().getresource("somefile.ext"); pattern to look for resources in src/test/resources where the resource file was in the same package location in the resources source folder as the test class was in the the test source folder. it still failed to locate them.

right click on the src/test/resources source folder, build path, then "configure inclusion / exclusion filters"; i added a new inclusion filter of **/*.ext to make sure my files weren't getting scrubbed; my tests now can find their resource files.


if you are using a java maven project in netbeans (not ant), then place your file as following:


notice the location of the resources folder. you can also have nested folders within the resources. then you can access your file as following,

url url = this.getclass().getresource("/install.xml");

more details:


i think i did encounter the same issue as yours. i created a simple mvn project and used "mvn eclipse:eclipse" to setup a eclipse project.

for example, my source file "" locates in "java/main/org/jhoh/mvc". and wants to read file "routes" which locates in "java/main/org/jhoh/mvc/resources"

i run "" in eclipse, and eclipse's console got nullpointerexeption. i set pom.xml with this setting to make all *.class java bytecode files locate in build directory.


i went to directory "build/classes/org/jhoh/mvc/resources", and there is no "routes". eclipse did not copy "routes" to "build/classes/org/jhoh/mvc/resources"

i think you can copy your "install.xml" to your *.class bytecode directory, not in your source code directory.


i had the same issue with the following conditions:

  • the resource files are in the same package as the java source files, in the java source folder (src/test/java).
  • i build the project with maven on the command line and the build failed on the tests with the nullpointerexception.
  • the command line build did not copy the resource files to the test-classes folder, which explained the build failure.
  • when going to eclipse after the command line build and rerun the tests in eclipse i also got the nullpointerexception in eclipse.
  • when i cleaned the project (deleted the content of the target folder) and rebuild the project in eclipse the test did run correctly. this explains why it runs when you start with a clean project.

i fixed this by placing the resource files in the resources folder in test: src/test/resources using the same package structure as the source class.

btw i used getclass().getresource(...)


when eclipse runs the test case it will look for the file in target/classes not src/test/resources. when the resource is saved eclipse should copy it from src/test/resources to target/classes if it has changed but if for some reason this has not happened then you will get this error. check that the file exists in target/classes to see if this is the problem.


it should be getresource("/install.xml");

the resource names are relative to where the getclass() class resides, e.g. if your test is org/example/foo/mytest.class then getresource("install.xml") will look in org/example/foo/install.xml.

if your install.xml is in src/test/resources, it's in the root of the classpath, hence you need to prepend the resource name with /.

also, if it works only sometimes, then it might be because eclipse has cleaned the output directory (e.g. target/test-classes) and the resource is simply missing from the runtime classpath. verify that using the navigator view of eclipse instead of the package explorer. if the files is missing, run the mvn package goal.



  • when you use .getclass().getresource(filename) it considers the location of the filename is the same location of the of the calling class.
  • when you use .getclass().getclassloader().getresource(filename) it considers the location of the filename is the root - in other words bin folder.

source :

package sound;
public class resourcetest {
    public static void main(string[] args) {
        string filename = "kalimba.mp3";
        system.out.println(new resourcetest().getclass().getresource(filename));
        system.out.println(new resourcetest().getclass().getclassloader().getresource(filename));

output :


Related Query

More Query from same tag