简体   繁体   English

如何将资源目录的路径添加到可执行jar

[英]How to add path to resource directory to an executable jar

I am trying to create an executable jar (code.jar) that depends on another jar file (other.jar) that I create in a separate project(=directory). 我正在尝试创建一个依赖于我在单独的项目(=目录)中创建的另一个jar文件(other.jar)的可执行jar(code.jar)。

The problem I'm having is that there is a class in other.jar that looks for an image file (xyz.gif) that is contained within that project: eg, project 'other' looks like: build/ images/xyz.gif src/... etc. 我遇到的问题是,other.jar中有一个类正在寻找该项目中包含的图像文件(xyz.gif):例如,项目“ other”看起来像:build / images / xyz.gif src / ...等

I make other.jar (including the /images directory) and then I make code.jar. 我创建other.jar(包括/ images目录),然后创建code.jar。

However, when I run java -jar code.jar - it is unable to locate xyz.gif: java.lang.IllegalArgumentException: input == null! 但是,当我运行java -jar code.jar时,无法找到xyz.gif:java.lang.IllegalArgumentException:输入== null! at javax.imageio.ImageIO.read(ImageIO.java...) 在javax.imageio.ImageIO.read(ImageIO.java ...)

I have tried everything I can think of to communicate the path to images/xyz.gif to the jar, including: 1. I tried adding the CLASS-PATH: images/ to the MANIFEST.MF for other.jar 2. I tried copying the images directory into the build for code.jar and adding CLASS-PATH: images/ to the MANIFEST.MF for code.jar 3. I tried putting CLASS-PATH: images/xyz.gif - in both manifest files 我已经尝试了所有可以想到的将images / xyz.gif的路径传达到jar的方法,包括:1.我尝试将CLASS-PATH:images /添加到MANIFEST.MF中,以用于other.jar 2.我尝试复制将images目录放入code.jar的构建中,并将CLASS-PATH:images /添加到code.jar的MANIFEST.MF中。3.我尝试将CLASS-PATH:images / xyz.gif-放在两个清单文件中

This seems like a general problem: How to include a (non-class) resource (eg, an image file) to a java jar file in such a way that java can locate it (without subsequent packages that utilize the sub-package - eg, code.jar uses other.jar) needing to know the details. 这似乎是一个普遍的问题:如何以Java可以找到它的方式将(非类)资源(例如,图像文件)包含到Java jar文件中(不使用子包的后续包,例如,code.jar使用other.jar)需要了解详细信息。

Say I can successfullly run the code project using: 说我可以使用以下命令成功运行代码项目:

java -cp somepath/images com.xyz.code java -cp somepath / images com.xyz.code

What I want to do is run: 我想做的是运行:

java -jar code.jar - and have it locate the images/ directory on the classpath. java -jar code.jar-并在类路径中找到images /目录。

I should add that the image-containing jar (other.jar) project is not my code - I am just trying to compile it to use. 我应该补充一点,包含图像的jar(other.jar)项目不是我的代码-我只是尝试对其进行编译以使用。 That code tries to load the image using: javax.imageio.ImageIO.read(ClassLoader.getSystemResource("xyz.gif"); There seems to be a lot of discussion about whether this is the correct/best way to load an image - I assume that the authors of this code DID have it working using Eclipse (whi ch likely sorted out the paths), and I simply want to get it working on the command line (and eventually in ant). 该代码尝试使用以下方法加载图像:javax.imageio.ImageIO.read(ClassLoader.getSystemResource(“ xyz.gif”);似乎有很多关于这是否是加载图像的正确/最佳方法的讨论-我假设此代码DID的作者可以使用Eclipse进行工作(可能会整理出路径),而我只是想使其在命令行上工作(并最终在ant中)。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I don't understand why the documentation on jar creation is so minimal. 我不明白为什么jar创建文档如此之少。

Instead of running jar directly try putting it in classpath. 而不是直接运行jar,请尝试将其放在classpath中。 Something like this: 像这样:

java -cp code.jar MainClass java -cp code.jar MainClass

This might work. 这可能有效。

EDIT: 编辑:

Above solution will not work. 上述解决方案将不起作用。 Try the below code to get image. 尝试以下代码获取图像。

ImageIcon myIcon = new ImageIcon( this.getClass().getResource("/resources/icon.gif"));

and place the image at below locations: 并将图像放置在以下位置:

/myApp/src/resources/icon.gif
/myApp/bin/resources/icon.gif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM