Jar将图像作为资源 [英] Jar get image as resource

查看:113
本文介绍了Jar将图像作为资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从我的罐子里加载一张图片。但无论我为getResource()提供什么字符串,它总是返回null。

I'm trying to get load an image from my jar. But no matter what string I supply for getResource() it always returns null.

try {
    System.out.println(Bootstrapper.class.getResource("./img/logo.png").toURI().getPath());
} catch (URISyntaxException ex) {
    Logger.getLogger(CrawlerFrame.class.getName()).log(Level.SEVERE, null, ex);


   }
   ImageIcon ii = new ImageIcon(Bootstrapper.class.getResource("./img/logo.png"));
   setIconImage(ii.getImage());




线程AWT-EventQueue-0java.lang中的异常。 NullPointerException
at net.sharpcode.crawler.ui.CrawlerFrame.init(CrawlerFrame.java:35)
at net.sharpcode.crawler.ui.CrawlerFrame。(CrawlerFrame.java:28)
at net.sharpcode.crawler.Bootstrapper $ 1.run(Bootstrapper.java:55)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at net.sharpcode.crawler.ui.CrawlerFrame.init(CrawlerFrame.java:35) at net.sharpcode.crawler.ui.CrawlerFrame.(CrawlerFrame.java:28) at net.sharpcode.crawler.Bootstrapper$1.run(Bootstrapper.java:55)

我试过:

getResource("") 
getResource(".") 
getResource("./") 
getResource("/img/logo.png") 
Bootstrapper.class.getProtectionDomain().getCodeSource().getLocation().getPath()


推荐答案

this.getClass().getResource("/net/sharpcode/crawler/img/logo.png")

这篇关于Jar将图像作为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆