Jar将图像作为资源

Rei*_*ard 2 java resources jar embedded-resource

我正试图从我的罐子里加载一个图像.但无论我为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());
Run Code Online (Sandbox Code Playgroud)

net.sharpcode.crawler.ui.CrawlerFrame中net.sharpcode.crawler.ui.CrawlerFrame.init(CrawlerFrame.java:35)中的线程"AWT-EventQueue-0"java.lang.NullPointerException中的异常.(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()
Run Code Online (Sandbox Code Playgroud)

And*_*son 5

this.getClass().getResource("/net/sharpcode/crawler/img/logo.png")
Run Code Online (Sandbox Code Playgroud)