我遇到了將我的java文件導出到可運行jar文件的問題。 在eclipse中它工作正常,但是當我導出它時,它不起作用。在Eclipse上工作但在導出到可運行jar文件時不工作的Java項目
我已經嘗試過(Java的罐子MyJar.jar)來獲取日誌,但它說 「Unnable訪問的jar文件」
我認爲這個問題是因爲這一點:
java.net.URL logoOneUrl = getClass().getResource("/logo.png"); //already tried without the "/" and it doesn't work withouth the "/"
Icon logoOne = new ImageIcon(logoOneUrl)
因爲當我把它放在評論//
當我導出它運行,但沒有圖像。
我也試過這種方式: java.net.URL logoScience = getClassLoader().getResource("logo.png");
但它不起作用。 我在做什麼錯? 如何導出JLabel上帶有圖像的可運行jar文件?
(這就是我對我的JLabel) JLabel lblImgLogin = new JLabel(logoOne);
UPDATE
ImageIcon icon = createImageIcon("/logo.png","a pretty but meaningless splat");
protected ImageIcon createImageIcon(String path,String description) { java.net.URL imgURL = getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL, description); } else { System.err.println("Couldn't find file: " + path); } return null; }
JLabel lblImgLogin = new JLabel(icon);
同樣的問題,廠在食,但在出口時要運行的JAR文件無法正常工作
該文件位於您的項目中? – 2015-02-11 11:04:37
「無法訪問jar文件」與代碼無關。它與當前目錄中不存在jar文件MyJar.jar的事實有關。 – 2015-02-11 11:05:26
@JBNizet但爲什麼它的工作,然後,如果他評論的形象? – 2015-02-11 11:07:34