如何将图像打包到Runnable JAR中 [英] how to package images into a Runnable JAR

查看:162
本文介绍了如何将图像打包到Runnable JAR中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前 ...(问题已删除)

Before... (question deleted)

我正在尝试从Swing项目中创建一个可运行的jar。

I'm trying to make a runnable jar from a Swing project.

我在/ img文件夹中添加了一些图像。以前的版本没有它,并且在Eclipse中导出runnable jar是好的。

I add some images in /img folder. Previous version didn't have it and runnable jar was good through export in Eclipse.

现在我想有些东西出了问题。在runnable jar中我添加了相同级别的主包和META-INF文件夹,这个img文件夹但似乎GUI没有出现。

Now I guess something is going wrong. In runnable jar I added at same level of main package and META-INF folder, this img folder but it seems GUI does not appear.

构建GUI之前的一些过程进展顺利所以主要课程似乎没问题。

Some process before building GUI went good so main class seems ok.

任何建议!?

谢谢。

评论:

在命令行运行它并发布你得到的错误。 - Kevin

Run it on the command line and post the error you get. – Kevin

任何建议!?是。发布jar文件的结构,失败的代码,以及你得到的异常的堆栈跟踪。否则,我看不出我们如何能帮到你。 - JB Nizet

Any suggestion!?. Yes. post the structure of the jar file, the code which fails, and the stack trace of the exception you get. Otherwise, I don't see how we could help you. – JB Nizet

您是否将一些图片放入类加载器可以看到的路径中? - 感知

Did you plop some images into a path the classloader can see? – Perception

之后 ....

所以我发现了问题,仍然不是解决方案:

So I found the problem, still not the solution:

正如我所说,我手动将/ img放入jar中,如第一个截图所示:

As I said, I manually put /img inside jar as you can see by first screenshot:

http://img94.imageshack.us /img94/1691/screenshot021ep.jpg

但没有任何反应,现在我把/ img放在jar和GUI开始之外:

But nothing happens, now I put /img outside jar and GUI starts:

http://img21.imageshack.us/img21/1551 /screenshot022jj.jpg

那么,我如何为我的应用程序打包/ img文件夹或图标?

So, how can I package /img folder or icons for my app?

谢谢。

PS:cmd行没有打印任何东西,应用程序启动并做了一些正确的事情,除了GUI构建。

PS: cmd line didn't print anything, the app starts and does some things right except for GUI building.

推荐答案

在Eclipse中,创建一个名为resources的新源文件夹。然后,将您的图像文件放在与您的类相同的包中,但是在resources文件夹中。通过执行此操作,您可以使用以下代码将图像作为输入流:

In Eclipse, create a new source folder called "resources". Then, put your image file in the same package as your class, but in your "resources" folder. By doing this, you can use the following code to get your image as input stream:

ClassName.class.getResourceAsStream(imageFileName);

这篇关于如何将图像打包到Runnable JAR中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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