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

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

问题描述

我制作了一个 Java 应用程序并将所有类捆绑在一个 jar 文件中.当我从 Eclipse 运行项目时,我的应用程序运行成功.但是当我尝试运行我的 .jar 文件时,我没有得到我的应用程序使用的图标.在代码中,我从项目文件夹中的图像目录中获取我的图标.使用 jar 时如何将这些图像文件呈现给最终用户?

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");

我也试过

final public ImageIcon iquit=new ImageIcon(getClass().getResource("images/quit.png"));

final public ImageIcon iquit=new ImageIcon(getClass().getResource("/images/quit.png"));

但这会导致错误:

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.

假设 images 实际上是一个包,并且这个包与当前类在同一个 JAR 中,那么这样做:

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天全站免登陆