當我建立一個jar文件並運行它的圖像,它顯示了由於ImageIcon的一個空指針異常找不到無法讀取jar文件
new ImageIcon(getClass().getClassLoader().getResource("icons/exit.png")));
這是錯誤當我運行jar是我所得到文件
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at mediaplayer.MediaPlayer.buildtoolbar(MediaPlayer.java:130)
at mediaplayer.MediaPlayer.<init>(MediaPlayer.java:81)
at mediaplayer.MediaPlayer.main(MediaPlayer.java:464)
但是當我運行在NetBeans中的項目,它的工作好
這是輸出時,我列出我的罐子裏面的所有文件
META-INF/
META-INF/MANIFEST.MF
helliker/
helliker/id3/
icons/
mediaplayer/
Thumbs.db
exit.png
ff1.png
helliker/id3/BinaryParser.class
helliker/id3/CorruptHeaderException.class
helliker/id3/ID3Exception.class
helliker/id3/ID3FieldDataException.class
helliker/id3/ID3Tag.class
helliker/id3/ID3v1Tag.class
helliker/id3/ID3v2ExtendedHeader.class
helliker/id3/ID3v2Footer.class
helliker/id3/ID3v2FormatException.class
helliker/id3/ID3v2Frame.class
helliker/id3/ID3v2Frames.class
helliker/id3/ID3v2Header.class
helliker/id3/ID3v2Tag.class
helliker/id3/MP3Comparator.class
helliker/id3/MP3File.class
helliker/id3/MP3FileFilter.class
helliker/id3/MPEGAudioFrameHeader.class
helliker/id3/NoMPEGFramesException.class
helliker/id3/NullsoftID3GenreTable.class
helliker/id3/Playlist.class
helliker/id3/PlaylistException.class
helliker/id3/XingVBRHeader.class
icons/Thumbs.db
icons/exit.png
icons/ff1.png
icons/label.jpg
icons/openpl.gif
icons/pause1.png
icons/play1.png
icons/playlist.png
icons/rr.png
icons/rr1.PNG
icons/stop.png
label.jpg
mediaplayer/MediaPlayer$1.class
mediaplayer/MediaPlayer$PlaylistFilter.class
mediaplayer/MediaPlayer.class
mediaplayer/PlaylistManager$1.class
mediaplayer/PlaylistManager$MP3Filter.class
mediaplayer/PlaylistManager$PlaylistFilter.class
mediaplayer/PlaylistManager.class
mediaplayer/Settings.class
mediaplayer/TagEditor.class
mediaplayer/Thumbs.db
openpl.gif
pause1.png
play1.png
playlist.png
rr.png
rr1.PNG
嘗試ClassLoader.class.getResource()。 – user1516873
將圖像打包到JAR中嗎? – 2012-11-06 11:13:06
您應該將jar文件作爲zip文件打開以確保圖標在那裏。 – HericDenis