I have created a minimal maven project with a single child module in eclipse helios.
In the src/test/resources folder I have put a single file "install.xml". In the folder src/test/java I have created a single package with a single 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 a junit 4 unit test I just get a NullPointerException. This has worked fine a million of times before. Any ideas?
I have followed this guide:
http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/
but still get the same error.
This question is related to
eclipse
maven-2
classloader
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 "Router.java" locates in "java/main/org/jhoh/mvc". And Router.java wants to read file "routes" which locates in "java/main/org/jhoh/mvc/resources"
I run "Router.java" 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.
<build>
<defaultGoal>package</defaultGoal>
<directory>${basedir}/build</directory>
<build>
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 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:
src/main/resources
I created the same structure for the packages in the resources directory too. For example:
If my class is on
com.package1.main
In the resources directory I put one package with the same name
com.package1.main
So I use
getClass().getResource("resource.png");
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.
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.
tul,
.getClass().getResource(fileName)
it considers the
location of the fileName is the same location of the of the calling
class..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(fileName);
System.out.println(new ResourceTest().getClass().getResource(fileName));
System.out.println(new ResourceTest().getClass().getClassLoader().getResource(fileName));
}
}
Output :
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
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.
I had the same issue with the following conditions:
src/test/java
).NullPointerException
.test-classes
folder, which explained the build failure.NullPointerException
in eclipse.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(...)
Source: Stackoverflow.com