如何使用eclipse来包括jar文件中的所有图像 [英] How to includes all images in jar file using eclipse
问题描述
.jar
文件时,我没有得到我的应用程序使用的图标。在代码中,我从项目文件夹中存在的图像目录中获取图标。如何在使用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));
但这会导致错误:
线程中的异常mainjava.lang.NullPointerException
在javax.swing.ImageIcon。< init>(未知来源)
您需要从类路径中获取它,而不是从本地磁盘文件系统获取。假设图像
实际上是一个包,并且该包与当前类在同一个JAR内,那么这样做: / p>
final public ImageIcon iReport =
new ImageIcon(getClass()。getResource(/ images / Report.png) );
I made a java application and bundled all classes in a jar file. When I run the project from eclipse, my application is running successfully. But when I try to run my .jar
file, I am not getting the icons used by my application. In the code I get my icons from images directory present in project folder. How can I present these image files to the end user when using a jar?
I am loading the image like so:
final public ImageIcon iReport=new ImageIcon("images/Report.png");
I have also tried
final public ImageIcon iquit=new ImageIcon(getClass().getResource("images/quit.png"));
and
final public ImageIcon iquit=new ImageIcon(getClass().getResource("/images/quit.png"));
But this results in an error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
You need to get it from the classpath instead of from the local disk file system.
Assuming that images
is actually a package and that this package is inside the same JAR as the current class, then do so:
final public ImageIcon iReport =
new ImageIcon(getClass().getResource("/images/Report.png"));
这篇关于如何使用eclipse来包括jar文件中的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!