使用Maven在jar中使用图像资源 [英] Using image resources in a jar using maven
问题描述
我有一个使用探戈图标的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"));