eclipse this.getClass().getClassLoader().getResource("...") 和 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
this.getClass().getClassLoader().getResource("...") and NullPointerException
提问by u123
I have created a minimal maven project with a single child module in eclipse helios.
我在 eclipse helios 中创建了一个带有单个子模块的最小 Maven 项目。
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:
在 src/test/resources 文件夹中,我放置了一个文件“install.xml”。在文件夹 src/test/java 中,我创建了一个包含单个类的包,它执行以下操作:
@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?
但是当我将代码作为 junit 4 单元测试运行时,我只会得到一个 NullPointerException。这在之前已经运行了一百万次。有任何想法吗?
I have followed this guide:
我遵循了本指南:
http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/
http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/
but still get the same error.
但仍然得到同样的错误。
回答by AmishDave
When you use
当你使用
this.getClass().getResource("myFile.ext")
getResource
will try to find the resource relative to the package.
If you use:
getResource
将尝试查找与包相关的资源。如果您使用:
this.getClass().getResource("/myFile.ext")
getResource
will treat it as an absolute path and simply call the classloader like you would have if you'd done.
getResource
将其视为绝对路径,并像完成后一样简单地调用类加载器。
this.getClass().getClassLoader().getResource("myFile.ext")
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.
不能/
在ClassLoader
路径中使用前导的原因是所有ClassLoader
路径都是绝对路径,因此/
不是路径中的有效第一个字符。
回答by namalfernandolk
tul,
图尔,
- 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 wordsbin
folder.
- 当您使用
.getClass().getResource(fileName)
它时,它认为 fileName 的位置与调用类的位置相同。 - 当您使用
.getClass().getClassLoader().getResource(fileName)
它时,它认为 fileName 的位置是根 - 换句话说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));
}
}
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
回答by mhaller
It should be getResource("/install.xml");
它应该是 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
.
资源名称与 getClass() 类所在的位置有关,例如,如果您的测试是org/example/foo/MyTest.class
然后getResource("install.xml")
将查找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 /
.
如果您install.xml
在 中src/test/resources
,则它位于类路径的根目录中,因此您需要在资源名称前加上/
.
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.
此外,如果它只是有时工作,那么可能是因为 Eclipse 已经清理了输出目录(例如target/test-classes
),并且资源只是从运行时类路径中丢失了。验证是否使用 Eclipse 的 Navigator 视图而不是 Package explorer。如果文件丢失,请运行mvn package
目标。
回答by oenpelli
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.
当 Eclipse 运行测试用例时,它将在目标/类而不是 src/test/resources 中查找文件。当资源被保存时,eclipse 应该将它从 src/test/resources 复制到 target/classes,如果它发生了变化,但如果由于某种原因这没有发生,那么你会得到这个错误。检查该文件是否存在于目标/类中以查看这是否是问题所在。
回答by Hilbrand Bouwkamp
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.
- 资源文件与 java 源文件在同一个包中,在 java 源文件夹 (
src/test/java
) 中。 - 我在命令行上使用 maven 构建项目,但在使用
NullPointerException
. - 命令行构建没有将资源文件复制到文件
test-classes
夹,这就解释了构建失败。 - 在命令行构建后进入 eclipse 并在 eclipse 中重新运行测试时,我也得到了
NullPointerException
in eclipse。 - 当我清理项目(删除目标文件夹的内容)并在 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.
我通过将资源文件放在测试中的资源文件夹中src/test/resources
来解决这个问题:使用与源类相同的包结构。
BTW I used getClass().getResource(...)
顺便说一句,我用过 getClass().getResource(...)
回答by Le Wang
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.
我想我确实遇到了和你一样的问题。我创建了一个简单的 mvn 项目并使用“mvn eclipse:eclipse”来设置一个 eclipse 项目。
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"
例如,我的源文件“Router.java”位于“java/main/org/jhoh/mvc”中。并且 Router.java 想要读取位于“java/main/org/jhoh/mvc/resources”中的文件“routes”
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.
我在 eclipse 中运行“Router.java”,eclipse 的控制台得到了 NullPointerExeption。我使用此设置设置 pom.xml 以使所有 *.class java 字节码文件位于构建目录中。
<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 NOTcopy "routes" to "build/classes/org/jhoh/mvc/resources"
我去了目录“build/classes/org/jhoh/mvc/resources”,没有“路线”。Eclipse没有将“路由”复制到“build/classes/org/jhoh/mvc/resources”
I think you can copy your "install.xml" to your *.class bytecode directory, NOTin your source code directory.
我认为您可以将您的“install.xml”复制到您的 *.class 字节码目录,而不是您的源代码目录中。
回答by Le Wang
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:
我在使用 Maven 的项目中遇到了同样的问题。我是如何修复它的:我只是将源(图像、音乐和其他东西)放在资源目录中:
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");
回答by Mikeb
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.
在 Eclipse / Maven 项目中,我有 Java 类,src/test/java
在其中我使用this.getClass().getResource("someFile.ext");
模式查找资源,src/test/resources
其中资源文件位于资源源文件夹中的同一包位置,而测试类位于测试源文件夹中. 它仍然无法找到它们。
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.
右键单击src/test/resources
源文件夹,构建路径,然后“配置包含/排除过滤器”;我添加了一个新的包含过滤器,**/*.ext
以确保我的文件不会被清理;我的测试现在可以找到它们的资源文件。