无法读取jar文件中的图像 [英] Cannot read an image in jar file
问题描述
当我构建一个jar文件并运行它时,由于找不到imageicon,它显示一个空指针异常
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")));
这是我运行jar文件时得到的错误
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)
但是当我在NetBeans中运行项目时,它的工作正常
But when i run the project in NetBeans, its working good
当我列出我的jar中的所有文件时,这是输出
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
推荐答案
在问题中如何实际构建jar文件时缺少一些信息,但是具有以下目录布局
There is missing some information in the question how the jar file is actually built, but with the following directory layout
├── bin
│ ├── com
│ │ └── example
│ │ └── ImageIconTest.class
│ └── icons
│ └── exit.png
└── src
├── MANIFEST.MF
└── com
└── example
└── ImageIconTest.java
以及以下公司de in ImageIconTest.java
and the following code in ImageIconTest.java
package com.example;
import javax.swing.ImageIcon;
public class ImageIconTest {
public void run() {
ImageIcon ii = new ImageIcon(getClass().getClassLoader().getResource("icons/exit.png"));
System.out.println(ii);
}
public static void main(String[] args) {
ImageIconTest app = new ImageIconTest();
app.run();
}
}
您可以使用文件系统正确运行示例
you can properly run the sample from the file system with
$ java -classpath bin com.example.ImageIconTest
使用带有以下内容的MANIFEST.MF文件:
Using a MANIFEST.MF file with the following content:
Manifest-Version: 1.0
Main-Class: com.example.ImageIconTest
你可以将它打包成一个可执行的jar文件并从jar文件中运行它:
you can package it into an executable jar file and run it from the jar file:
$ jar cvfm app.jar src/MANIFEST.MF -C bin .
$ java -jar app.jar
这两种方法都运行正常,重要的细节是确保图标目录包含在适当位置的jar文件中。
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.
列出jar文件内容时,应该看起来像这样:
When listing the jar file contents, it should look like this:
0 Tue Nov 06 12:27:56 CET 2012 META-INF/
107 Tue Nov 06 12:27:56 CET 2012 META-INF/MANIFEST.MF
0 Tue Nov 06 12:27:56 CET 2012 com/
0 Tue Nov 06 12:27:56 CET 2012 com/example/
950 Tue Nov 06 12:27:56 CET 2012 com/example/ImageIconTest.class
0 Tue Nov 06 12:00:36 CET 2012 icons/
873 Tue Nov 06 12:00:36 CET 2012 icons/exit.png
注意图标目录的位置。
这篇关于无法读取jar文件中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!