如何使用eclipse来包括jar文件中的所有图像 [英] How to includes all images in jar file using eclipse

查看:120
本文介绍了如何使用eclipse来包括jar文件中的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个java应用程序,并将jar文件中的所有类捆绑在一起。当我从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆