Jar 获取图像作为资源 [英] Jar get image as resource
问题描述
我正在尝试从我的 jar 中加载图像.但无论我为 getResource() 提供什么字符串,它总是返回 null.
尝试{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-0"中的异常java.lang.NullPointerException在 net.sharpcode.crawler.ui.CrawlerFrame.init(CrawlerFrame.java:35)在 net.sharpcode.crawler.ui.CrawlerFrame.(CrawlerFrame.java:28)在 net.sharpcode.crawler.Bootstrapper$1.run(Bootstrapper.java:55)
我试过了:
getResource("")获取资源(.")获取资源(./")getResource("/img/logo.png")Bootstrapper.class.getProtectionDomain().getCodeSource().getLocation().getPath()
this.getClass().getResource("/net/sharpcode/crawler/img/logo.png")
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());
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)
I've tried:
getResource("")
getResource(".")
getResource("./")
getResource("/img/logo.png")
Bootstrapper.class.getProtectionDomain().getCodeSource().getLocation().getPath()
this.getClass().getResource("/net/sharpcode/crawler/img/logo.png")
这篇关于Jar 获取图像作为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!