我製作了一個Java應用程序,並將所有類都捆綁在一個jar文件中。當我從eclipse運行項目時,我的應用程序運行成功。但是,當我嘗試運行我的.jar
文件時,我沒有收到我的應用程序使用的圖標。在代碼中,我從圖像目錄中獲取項目文件夾中的圖標。如何在使用jar時將這些圖像文件呈現給最終用戶?如何使用eclipse在jar文件中包含所有圖像
我加載像這樣的形象:
final public ImageIcon iReport=new ImageIcon("images/Report.png");
我也曾嘗試
final public ImageIcon iquit=new ImageIcon(getClass().getResource("images/quit.png"));
和
final public ImageIcon iquit=new ImageIcon(getClass().getResource("/images/quit.png"));
但是這將導致一個錯誤:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
告訴我們一些代碼?如何加載它有時可用,但不是其他人? – 2011-06-16 13:55:33
您應該查看您如何訪問代碼中的圖像資源。確保它在jar文件中可以訪問相對URL! – gotomanners 2011-06-16 13:55:55
你可能看看class.getResourceAsStream() – McPepper 2011-06-16 14:04:47