简体   繁体   English

如何将图像打包成Runnable JAR

[英]how to package images into a Runnable JAR

Before ... (question deleted) 之前 ......(问题已删除)

I'm trying to make a runnable jar from a Swing project. 我正在尝试从Swing项目中创建一个可运行的jar。

I add some images in /img folder. 我在/ img文件夹中添加了一些图像。 Previous version didn't have it and runnable jar was good through export in Eclipse. 以前的版本没有它,并且在Eclipse中导出runnable jar很好。

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. 在runnable jar我添加了相同级别的主包和META-INF文件夹,这个img文件夹但似乎GUI没有出现。

Some process before building GUI went good so main class seems ok. 构建GUI之前的一些过程进展顺利,因此主要类似乎没问题。

Any suggestion!? 有什么建议!?

Thanks. 谢谢。

Comments : 评论 :

Run it on the command line and post the error you get. 在命令行上运行它并发布您获得的错误。 Kevin - 凯文

Any suggestion!?. 有什么建议!? Yes. 是。 post the structure of the jar file, the code which fails, and the stack trace of the exception you get. 发布jar文件的结构,失败的代码,以及你得到的异常的堆栈跟踪。 Otherwise, I don't see how we could help you. 否则,我看不出我们如何能帮到你。 JB Nizet - JB Nizet

Did you plop some images into a path the classloader can see? 你有没有把一些图像放到类加载器可以看到的路径中? Perception - 感知

After .... 之后 ....

So I found the problem, still not the solution: 所以我发现了问题,仍然不是解决方案:

As I said, I manually put /img inside jar as you can see by first screenshot: 正如我所说,我通过第一个截图手动将/ img手动放入jar中:

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

But nothing happens, now I put /img outside jar and GUI starts: 但没有任何反应,现在我把/ img放在jar和GUI之外:

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

So, how can I package /img folder or icons for my app? 那么,我如何为我的应用程序打包/ img文件夹或图标?

Thanks. 谢谢。

PS: cmd line didn't print anything, the app starts and does some things right except for GUI building. PS:cmd行没有打印任何内容,应用程序启动并做了一些正确的事情,除了GUI构建。

In Eclipse, create a new source folder called "resources". 在Eclipse中,创建一个名为“resources”的新源文件夹。 Then, put your image file in the same package as your class, but in your "resources" folder. 然后,将您的图像文件放在与您的类相同的包中,但在“resources”文件夹中。 By doing this, you can use the following code to get your image as input stream: 通过执行此操作,您可以使用以下代码将图像作为输入流:

ClassName.class.getResourceAsStream(imageFileName);

i used to had same problems with accessing images 我以前在访问图像方面遇到了同样的问题

Check out the below link. 看看下面的链接。 It provides answers on 它提供了答案

  1. Where should i put my images? 我应该把我的图片放在哪里?
  2. How can access them? 如何访问它们?

Hope it helps 希望能帮助到你

link : http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html 链接: http//docs.oracle.com/javase/tutorial/uiswing/components/icon.html

Specifically check the section "Loading Images Using getResource " 具体检查“ 使用getResource加载图像”部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM