使用Maven在jar中使用图像资源 [英] Using image resources in a jar using maven

查看:142
本文介绍了使用Maven在jar中使用图像资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用探戈图标的java/maven项目,所以我在使用以下依赖项:

I have a java/maven project that uses tango icons, so I'm using the following dependency:

<dependency>
    <groupId>org.freedesktop.tango</groupId>
    <artifactId>tango-icon-theme</artifactId>
    <version>0.8.90</version>
</dependency>

下面的代码,在我将proyect移至Maven之前可以正常工作:

And the following code, which would work ok before I moved the proyect to maven:

imageIcon = new ImageIcon(this.getClass().getResource(org/freedesktop/tango/22x22/actions/address-book-new.png));

但是,由于某种原因,它现在导致NullPointerException. 生成的jar不包含图像,也不以任何方式引用它们,但是其他jar中的类是.

However, it now results in NullPointerException for some reason. The generated jar does not contain the images, nor reference them in any way, but classes in other jars are.

我已经进行了很多调查,但是我发现大多数人都将其图片与来源捆绑在一起.这不是我的情况.

I've investigated a lot, but I've found most people bundle their images with their source. This isn't my case.

我需要做什么才能使maven正确引用和/或打包这些图像? 我如何才能使maven生成一个独立的可运行jar(这是一个swing应用程序).

What do I need to do for maven to reference and/or package these images propertly? How can I get maven to build a standalone runnable jar (this is a swing application).

推荐答案

如果在路径起作用之前添加斜杠:

If you add a slash before the path it works:

ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/org/freedesktop/tango/22x22/actions/address-book-new.png"));

在委派之前,绝对资源名称是根据 使用以下算法指定资源名称:

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

如果名称以'/'开头('\ u002f'),则绝对名称为 资源是名称中'/'之后的部分.否则, 绝对名称的格式如下:
modified_pa​​ckage_name/name,其中Modifyed_pa​​ckage_name是 该对象的程序包名称,用"/"代替.". ('\ u002e').

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the following form:
modified_package_name/name Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

这篇关于使用Maven在jar中使用图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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