Java getClassLoader().getResource() 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/625687/
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
getClassLoader().getResource() returns null
提问by Click Upvote
I have this test app:
我有这个测试应用程序:
import java.applet.*;
import java.awt.*;
import java.net.URL;
public class Test extends Applet
{
public void init()
{
URL some=Test.class.getClass().getClassLoader().getResource("/assets/pacman.png");
System.out.println(some.toString());
System.out.println(some.getFile());
System.out.println(some.getPath());
}
}
When I run it from Eclipse, I get the error:
当我从 Eclipse 运行它时,出现错误:
java.lang.NullPointerException
at Test.init(Test.java:9)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Classpath (from .CLASSPATH file)
类路径(来自 .CLASSPATH 文件)
<classpathentry kind="src" path="src"/>
In my c:\project\src folder, I have only the Test.java file and the 'assets' directory which contains pacman.png.
在我的 c:\project\src 文件夹中,我只有 Test.java 文件和包含 pacman.png 的“assets”目录。
What am I doing wrong and how to resolve it?
我做错了什么以及如何解决它?
采纳答案by TofuBeer
I would do it this way:
我会这样做:
final InputStream stream;
stream = Test.class.getResourceAsStream("assets/pacman.png");
System.out.println("Stream = " + stream);
"/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.
"/assets/pacman.png" 是绝对位置,而 "assets/pacman.png" 是相对位置。
回答by Jon Skeet
You don't need the slash at the start when getting a resource from a ClassLoader
, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class
where relative paths go from the class's package level.
从 a 获取资源时,开始时不需要斜线ClassLoader
,因为不知道要从“相对”部分开始。只有当您Class
从类的包级别的相对路径获取资源时才需要它。
In addition, you don't want Test.class.getClass()
as that gets the class ofTest.class, which will be Class<Class>
.
此外,你不想Test.class.getClass()
为获取类的的Test.class,这将是Class<Class>
。
In other words, try either of these lines:
换句话说,请尝试以下任一行:
URL viaClass=Test.class.getResource("/assets/pacman.png");
URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");
回答by namalfernandolk
Click Upvote,
点击点赞,
- 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
- 当您使用
.getClass().getResource(fileName)
它时,它认为 fileName 的位置与调用类的位置相同。 - 当您使用
.getClass().getClassLoader().getResource(fileName)
它时,它认为 fileName 的位置是根 - 换句话说 bin 文件夹
It hits NullPointerException
if the file is actually not exist there.
NullPointerException
如果该文件实际上不存在,则会命中。
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 Thinhbk
This works for me:
这对我有用:
URL viaClass=Test.class.getResource("assets/test.html");
URL viaClass=Test.class.getResource("assets/test.html");
which assets in the same folder with Test.class output file (after a miserable checking and debugging)
与Test.class输出文件在同一个文件夹中的assets(经过惨痛的检查和调试)