我正在尝试从我的 jar 中加载图像。但无论我为 getResource() 提供什么字符串,它总是返回 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)
我已经尝试过:
getResource("")
getResource(".")
getResource("./")
getResource("/img/logo.png")
Bootstrapper.class.getProtectionDomain().getCodeSource().getLocation().getPath()
最佳答案
this.getClass().getResource("/net/sharpcode/crawler/img/logo.png")
关于java - Jar 获取图像作为资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10163012/