.jar文件可以找到图像文件路径

发布于 2025-01-19 06:51:08 字数 1749 浏览 2 评论 0原文

我正在创建一个简单的程序,现在想要将其导出为可执行 jar 文件。除了 jar 文件找不到图像之外,一切正常。

为了创建可运行的 JAR 文件,我使用了 Eclipse 2021-12 中的第二个选项(将所需的库打包到生成的 JAR 中)。这似乎工作得很好,因为我只有 jar 而没有运行该程序所需的其他文件夹。

我的 jar 文件结构如下所示:
jar_file/
├─ 图片/
│ ├─ frog.png

我尝试了多种获取图像文件的方法。

这就是我定义图标的方式:
私有静态图像图标;

使用toURI()和toString()

icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/frog.png").toURI ().toString());

Eclipse

这在 Eclipse 中不起作用。

路径输出:file:/C:/Projekte/serviceticketapp/target/classes/images/frog.png

Jar 文件

这在 Jar 文件中不起作用。

路径输出:rsrc:images/frog.png

使用 toExternalForm()

icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/frog.png")。 toExternalForm());

Eclipse

这在 Eclipse 中不起作用。

路径输出:file:/C:/Projekte/serviceticketapp/target/classes/images/frog.png

Jar 文件

这在 Jar 文件中不起作用。

路径输出:rsrc:images/frog.png

使用 File 和 toURI()

File imageFile = new File(getClass().getResource("/images/frog.png").toURI( )); icon = Toolkit.getDefaultToolkit().getImage(imageFile.getAbsolutePath());

Eclipse

这适用于 Eclipse。

路径输出:C:\Projekte\serviceticketapp\target\classes\images\frog.png

Jar 文件

抛出错误。

java.lang.IllegalArgumentException:URI 不是分层的

摘要

在上面您可以看到我尝试获取图像文件的所有方法。只有一个可以在 Eclipse 中工作,而没有一个可以在 jar 文件中工作。

我做错了什么或者我必须如何排列文件才能使其在 jar 文件和 Eclipse 中工作。

请询问您是否需要更多信息。提前致谢。

I'm creating a simple program that I now want to export as a executable jar file. Everything works fine except the jar file can't find the image.

For creating the runnable JAR file I used the second option in Eclipse 2021-12 (Package required libraries into generated JAR). This seems to work well, because I only have the jar and no other folders I need to run the program.

My jar file structure looks like this:
jar_file/
├─ images/
│ ├─ frog.png

I've tried multiple ways of getting the image file.

This is how I define Icon:
private static Image icon;

Using toURI() and toString()

icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/frog.png").toURI().toString());

Eclipse

This does not work in Eclipse.

Path Output: file:/C:/Projekte/serviceticketapp/target/classes/images/frog.png

Jar File

This does not work in the Jar File.

Path Output: rsrc:images/frog.png

Using toExternalForm()

icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/frog.png").toExternalForm());

Eclipse

This does not work in Eclipse.

Path Output: file:/C:/Projekte/serviceticketapp/target/classes/images/frog.png

Jar File

This does not work in the Jar File.

Path Output: rsrc:images/frog.png

Using File and toURI()

File imageFile = new File(getClass().getResource("/images/frog.png").toURI());
icon = Toolkit.getDefaultToolkit().getImage(imageFile.getAbsolutePath());

Eclipse

This works in Eclipse.

Path Output: C:\Projekte\serviceticketapp\target\classes\images\frog.png

Jar File

Throws error.

java.lang.IllegalArgumentException: URI is not hierarchical

Summary

Above you can see all the ways I tried to get the image file. Only one works in Eclipse and none of them work in the jar file.

What am I doing wrong or how do I have to arrange the files in order for it to work in the jar file and in Eclipse.

Please ask if you need more information. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

不顾 2025-01-26 06:51:08

我会使用 imagio (import javax.imageio.ImageIO)

tIcon = new TrayIcon(ImageIO.read(getClass().getResource("/images/frog.png"), "Service Ticket App");

I would use imagio (import javax.imageio.ImageIO)

tIcon = new TrayIcon(ImageIO.read(getClass().getResource("/images/frog.png"), "Service Ticket App");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文