This.getClass (). getClassLoader (). getResource ("...") and NullPointerException

I created a minimal maven project with one child module in eclipse heliums.

In the src / test / resources folder, I placed one file "install.xml". In the src / test / java folder, I created one package with one class that does:

@Test public void doit() throws Exception { URL url = this.getClass().getClassLoader().getResource("install.xml"); System.out.println(url.getPath()); } 

but when I run the code as junit 4 unit test, I just get a NullPointerException. It worked a million times. Any ideas?

I followed this guide:

http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/

but still get the same error.

+46
eclipse maven-2 classloader
Sep 27 '10 at 11:24
source share
8 answers

When you use

 this.getClass().getResource("myFile.ext") 

getResource will try to find the resource relative to the package. If you use:

 this.getClass().getResource("/myFile.ext") 

getResource will treat it as an absolute path and simply call the class loader, as if you did.

 this.getClass().getClassLoader().getResource("myFile.ext") 

The reason you cannot use the leading / in the ClassLoader path is because all the ClassLoader are absolute and therefore / not a valid first character in the path.

+72
Aug 17 '11 at 19:44
source share

tul,

  • When you use .getClass().getResource(fileName) , it considers the file location file_name to be the same location of the calling class.
  • When you use .getClass().getClassLoader().getResource(fileName) , this considers the location of the fileName to be the root - in other words bin .

Source:

 package Sound; public class ResourceTest { public static void main(String[] args) { String fileName = "Kalimba.mp3"; System.out.println(fileName); System.out.println(new ResourceTest().getClass().getResource(fileName)); System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName)); } } 

Exit:

 Kalimba.mp3 file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Sound/Kalimba.mp3 file:/C:/Users/User/Workspaces/MyEclipse%208.5/JMplayer/bin/Kalimba.mp3 
+28
Nov 29 2018-11-11T00:
source share

This should be getResource("/install.xml");

Resource names refer to where the getClass () class is located, for example. 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 is in the root path of the class path, so you need to add the resource name with / .

In addition, if it works only occasionally, this may be due to the fact that Eclipse cleared the output directory (for example, target/test-classes ), and the resource is simply missing from the path to the runtime. Make sure to use the Navigator view for Eclipse instead of the package explorer. If the files are missing, run the mvn package target.

+16
Sep 27 '10 at 11:58
source share

I had the same problem with the following conditions:

  • Resource files are in the same package as the java source files in the java source folder ( src/test/java ).
  • I am building a project with maven on the command line, and the build failed when testing with NullPointerException .
  • The command line did not copy resource files to the test-classes folder, which explained assembly failure.
  • When you are going to outshine after building the command line and retry the tests in eclipse, I also got a NullPointerException in eclipse.
  • When I cleaned up the project (deleted the contents of the target folder) and rebuilt the project in Eclipse, the test ran correctly. This explains why it starts when you start with a clean project.

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

By the way, I used getClass().getResource(...)

+3
May 22 '12 at
source share

When eclipse runs a 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 did not happen, you will get this error. Make sure the file exists in target / classes to make sure this is a problem.

+2
Oct 30 '13 at 6:41
source share

I think I ran into the same problem as yours. I created a simple mvn project and used "mvn eclipse: eclipse" to install the eclipse project.

For example, my source file "Router.java" is located in "java / main / org / jhoh / mvc". And Router.java wants to read the "routes" file, which is located in "java / main / org / jhoh / mvc / resources"

I run "Router.java" in eclipse, and the eclipse console got NullPointerExeption. I installed pom.xml with this option so that all * .class java bytecode files are in the build directory.

 <build> <defaultGoal>package</defaultGoal> <directory>${basedir}/build</directory> <build> 

I went to the directory "build / classes / org / jhoh / mvc / resources" and there are 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 to the source code directory.

+1
May 16 '13 at 12:24
source share

I had the same problem as the project with Maven. Here's how I fixed it: I just put the sources (images, music and other materials) in the resource directory:

 src/main/resources 

I created the same structure for packages in the resource directory. For example:

If my class is on

 com.package1.main 

In the resource directory I put one package with the same name

 com.package1.main 

Therefore i use

 getClass().getResource("resource.png"); 
0
Jun 18 '12 at 5:45
source share

One more thing to see what this resolved for me:

In the Eclipse / Maven project, I had Java classes in src/test/java in which I used the this.getClass().getResource("someFile.ext"); to search for resources in src/test/resources , where the resource file was located in one place of the package in the resource source as a test class in the test source folder. Still could not find them.

Right-click on the source folder src/test/resources , “Build Path”, then “configure inclusion / exclusion filters”; I added a new **/*.ext inclusion filter to make sure my files are not being cleaned; my tests can now find resource files.

0
Feb 02 '15 at 21:27
source share



All Articles