Java:在jar应用程序中设置图标

发布于 2024-12-06 06:00:57 字数 1229 浏览 1 评论 0原文

我开发了一个应用程序,将其导出到可运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演多会厌 2024-12-13 06:00:57

根据您使用的路径,您的图像需要放置在 jar 文件的根目录下。假设您的图像位于项目中名为“images”的目录中,则此 ANT 任务会将图像放在 jar 的根目录中:

<target name="construct-jar" depends="compile,javadoc">
    <copy todir="${build.dir}">
        <fileset dir="images"/>
    </copy>
    <jar destfile="${dist.dir}/${jar.name}" basedir="${build.dir}"/>
</target>

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:

<target name="construct-jar" depends="compile,javadoc">
    <copy todir="${build.dir}">
        <fileset dir="images"/>
    </copy>
    <jar destfile="${dist.dir}/${jar.name}" basedir="${build.dir}"/>
</target>
心的位置 2024-12-13 06:00:57

如果我将文件放在 .class 文件所在的位置,我应该像这样修改我的代码:

image = ImageIO.read(this.getClass().getResource("/AT42.png"));

谢谢!

If I put the file where the .class files are I should modify my code like this:

image = ImageIO.read(this.getClass().getResource("/AT42.png"));

Thanks!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文