从.jar文件运行时无法在Java Swing中加载图像 [英] Can not load the images in Java Swing when running from a .jar-file

查看:115
本文介绍了从.jar文件运行时无法在Java Swing中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以毫无问题地从Eclipse运行我的Java Swing应用程序。但是当我从.jar文件中运行它时,图像无法加载。

I can run my Java Swing application from Eclipse without problems. But when I run it from a .jar-file, the images fails to load.

我加载了我的图像:

setIconImage(Toolkit.getDefaultToolkit().
getImage(getClass().getResource("../images/logo.png")));

我如何加载图像,以便即使我从.jar文件运行它们也能工作?

How can I load the images so they work even when I'm running from a .jar-file?

图像位于同一个Jar文件中,包 com.example.images 和类使用它们的地方是 com.example.gui.dialogs

The images is in the same Jar-file, in the packet com.example.images and the class where they are used is in com.example.gui.dialogs

推荐答案

使用罐子里的绝对路径。如果您不知道,请尝试使用zip程序打开JAR,例如7zip的。然后使用绝对路径:

Use the absolut path inside your jar. If you don't know it, try opening the JAR with a zip programm, e.g. 7zip. Then use the absolute path:

getClass().getResource("/com/examples/images/logo.png")

只有当你的形象出现在你的罐子里时才会这样。如果不是,但在你的类路径中,这也应该没问题。

This obiously only works when your image is in your jar. If its not, but in your classpath, this should be fine too.

这篇关于从.jar文件运行时无法在Java Swing中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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