when i build a jar file and run it ,it shows a null pointer exception due to imageicon not found
new ImageIcon(getClass().getClassLoader().getResource("icons/exit.png")));
this is the error what i get when i run the jar file
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)
But when i run the project in NetBeans, its working good
This is the output when i list all the files inside my jar
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
The Exception is occurring when
MediaPlayer
in packagemediaplayer
calls for an embedded resource at"icons/exit.png"
. This would resolve to a path of:I am guessing that is not the path, which is actually.
That is why the
String
needs to be"/icons/exit.png"
- note the/
prefix.The
/
that precedes theString
informs the class-loader that we mean it to search for the resource from the root of the class path, as opposed to the package of the class from which it is called.There is missing some information in the question how the jar file is actually built, but with the following directory layout
and the following code in
ImageIconTest.java
you can properly run the sample from the file system with
Using a MANIFEST.MF file with the following content:
you can package it into an executable jar file and run it from the jar file:
Both approaches are working fine, the important detail is to make sure that the icon directory is included in the jar file at the proper location.
When listing the jar file contents, it should look like this:
Note the location of the icons directory.
Thanx a lot everyone. I figured out the answer
i removed the ClassLoader() and it worked,but i just dont know why. Can somebody please explain me the theory behind this.?
This code works well: