Java .Jar 图像未显示
我用 Java 创建了一个应用程序(使用 Eclipse IDE),并在代码中引用了存储在名为“source”的源文件夹中的图像,并且它在 IDE 中运行良好。当我将罐子提取到可运行的罐子时,没有错误,但图片没有显示,或者即使显示,也只是显示其后面的内容。我在 WinRar 中打开 .jar,看起来图片都与类文件一起扔了。我该如何解决这个问题?
Image i = Toolkit.getDefaultToolkit().getImage("sources/SystemTrayOne.png");
I made an application in Java (using the Eclipse IDE) and i refer in the code to images stored in a source folder called "source" and it worked fine in the IDE. When I extracted the jar to an runnable jar, there are no errors but the picture doesn't show up, or if it does it just shows whatever's behind it. I opened up the .jar in WinRar and it appears the pictures are all thrown in with the class files. How can I fix this?
Image i = Toolkit.getDefaultToolkit().getImage("sources/SystemTrayOne.png");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试获取 Jar 中不存在的文件。而是通过
Class#getResource(...)
获取 URL,例如,
或者更好
You're trying to get a file which doesn't exist in a Jar. Instead get the URL via the
Class#getResource(...)
e.g.,
or better
直到昨天,我也面临着同样的问题。我为此有修复。让我与您分享。
物理存在。
示例:xxx xxx = xxxx(example.jpg)用.... xxx xxx = xxxx(图像)更改它。 /example.jpg)
繁荣!运行.jar文件时将显示图像。
注意:不要使用Java -jar AppName.jar从CMD运行.jar文件,只需双击.jar文件即可看到图像。
如果对您有用,请务必投票。 :)
I was also facing the same issue till yesterday. I have got a fix for this. Let me share it with you.
is physically present.
Example : XXX xxx = XXXX (example.jpg) Change it with .... XXX xxx = XXXX (images/example.jpg)
BOOM !! The images will be shown when you run the .jar file.
NOTE : Do not run the .jar file from cmd using java -jar AppName.jar Instead, just double click the .jar file and you will be seeing the images.
If it works for you, kindly upvote. :)