从 Jar 加载图像:始终为 null
看了SO上的其他帖子,他们没有解决这个问题。
我正在尝试从我的 jar 文件加载图像。它不断地出现为空。该图像位于:
.Jar file > images > BLOCK.png
要加载我正在执行的图像:
BufferedImage bImg;
URL url = getClass().getResource("/images/BLOCK.png");
try {
bImg = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(TileEngine.class.getName()).log(Level.SEVERE, null, ex);
}
url 为 null,bImg 也是如此。
不要担心区分大小写,因为我已经检查过了。
Looked at other posts on SO and they did not solve this issue.
I'm trying to load an image from my jar file. It is continuously coming up as null. The image is located under:
.Jar file > images > BLOCK.png
To load the image I am doing:
BufferedImage bImg;
URL url = getClass().getResource("/images/BLOCK.png");
try {
bImg = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(TileEngine.class.getName()).log(Level.SEVERE, null, ex);
}
url is null as is bImg.
Do not worry about case sensitivity as I've already checked that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
try this :
与您用来调用 getResource 的类相比,图像位于哪个 jar 文件中?如果它们是由同一个类加载器加载的,那就应该没问题。
您是否仔细检查过 jar 文件实际上包含该文件?
您确定您的类加载器实际上正在使用 jar 文件(例如,而不是直接在磁盘上使用 .class 文件)?
如果您有一个简短但完整的程序来演示该问题,那将非常有帮助。 (控制台应用程序是理想的选择......毕竟我们不需要看到图像。)
Which jar file is the image in, compared with the class you're using to call
getResource
? If they're loaded by the same classloader, it should be fine.Have you double-checked that the jar file actually contains the file?
Are you sure that your classloader is actually using the jar file at all (rather than .class files directly on disk, for example)?
If you have a short but complete program which demonstrates the problem, that would really help. (A console app would be ideal... we don't need to see the image, after all.)