Java:在jar应用程序中设置图标
我开发了一个应用程序,将其导出到可运行的 jar 中(包括它需要的库)。一切正常。
从 Eclipse 运行应用程序时,我可以更改应用程序窗口显示的图标:
BufferedImage image = null;
try {
image = ImageIO.read(this.getClass().getResource("AT42.png"));
} catch (IOException e) {e.printStackTrace();}
this.setIconImage(image);
图片放置在我的 .class 文件目录中。
当我从 Eclipse 运行它时,它显示图标,但是当我创建一个可运行的 jar 并执行它时,我得到以下异常:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.tools.at4.UserInterface.<init>(UserInterface.java:43)
at com.tools.at4.GeneradorInformes.main(GeneradorInformes.java:8)
... 5 more
我猜图标不包含在 jar 文件中,我的问题是,有没有办法包含它,这样当我运行 jar 文件时,创建的窗口会显示我的图标而不是 Java 杯?
谢谢!!
I've developed an application that I export into a runnable jar (including the libraries it needs). Everything works fine.
When running the app from Eclipse I'm able to change the icon that the application window shows:
BufferedImage image = null;
try {
image = ImageIO.read(this.getClass().getResource("AT42.png"));
} catch (IOException e) {e.printStackTrace();}
this.setIconImage(image);
The picture is placed in my .class files directory.
When I run it from Eclipse it shows the icon, but when I create a runnable jar and execute it I get the following exception:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.tools.at4.UserInterface.<init>(UserInterface.java:43)
at com.tools.at4.GeneradorInformes.main(GeneradorInformes.java:8)
... 5 more
I guess the icon is not include in the jar file, my question is, is there any way of incluiding it, so that when I run the jar file the windows that are created show my icon instead of the Java cup?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您使用的路径,您的图像需要放置在 jar 文件的根目录下。假设您的图像位于项目中名为“images”的目录中,则此 ANT 任务会将图像放在 jar 的根目录中:
With the path you are using your image needs to be put at the root of your jar file. Assuming you have your image in a directory in your project called "images", this ANT task would put the image(s) at the root of your jar:
如果我将文件放在 .class 文件所在的位置,我应该像这样修改我的代码:
谢谢!
If I put the file where the .class files are I should modify my code like this:
Thanks!!