Eclipse:导出到 .jar 并包含资源文件 (ANT)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2073250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse: export to .jar AND include resource files (ANT)
提问by Atmocreations
Our project in eclipse approximately shows the following folders:
我们在 eclipse 中的项目大约显示了以下文件夹:
application
- src
- JRE System Library [1.6]
- Referenced Libraries
- lib
- rsc
In our project, we would like to use File > Export... > Executable JAR
在我们的项目中,我们想使用 File > Export... > Executable JAR
Well that works fine, with some exception: If we want to run our application.jar, we still need to copy the folder rsc/ in the same location as application.jar.
这很好,但有一些例外:如果我们想运行我们的 application.jar,我们仍然需要将文件夹 rsc/ 复制到与 application.jar 相同的位置。
In rsc/, we have other folders for separation of the different parts. Basically, we load the pieces using the code (well a bit altered, but the path style is correct)
在 rsc/ 中,我们还有其他文件夹用于分隔不同的部分。基本上,我们使用代码加载这些片段(有点改变,但路径样式是正确的)
strUrl = "file:rsc/properties/Constants.properties";
url = new URL(strUrl);
ImageIcon icon = new ImageIcon(url);
I could rightclick on rsc/ > Build Path > Use as Source Folder.
我可以右键单击 rsc/ > 构建路径 > 用作源文件夹。
But this doesn't work either because eclipse doesn't automatically copy the rsc folder into the bin/-folder. And then we cannot run it anymore from terminal (not packaged into jar)
但这也不起作用,因为 eclipse 不会自动将 rsc 文件夹复制到 bin/-folder 中。然后我们不能再从终端运行它(没有打包到 jar 中)
EDIT: why is eclipse unable to handle this correctly? Why does it have problems with "nested" output folders? In the Source-tab of the Build Path dialog, I could easily add the rsc/-folder with the output folder set to bin/rsc/, but it doesn't seem to be able to put nested folders...
编辑:为什么 eclipse 无法正确处理这个问题?为什么“嵌套”输出文件夹会出现问题?在“构建路径”对话框的“源”选项卡中,我可以轻松添加 rsc/-文件夹,并将输出文件夹设置为 bin/rsc/,但它似乎无法放置嵌套文件夹...
EDIT 2: Now I've been able to create an xml-file to build the stuff with ant, and somehow managed to include the rsc-folder right into the jar. Still doesn't run due to the same errors. Do I really need to check the resources paths whether they're in a JAR or not? The JAR's content is now the following:
编辑 2:现在我已经能够创建一个 xml 文件来用 ant 构建东西,并且以某种方式设法将 rsc 文件夹直接包含到 jar 中。由于相同的错误,仍然无法运行。我真的需要检查资源路径是否在 JAR 中吗?JAR 的内容现在如下:
META-INF/
META-INF/MANIFEST.MF
configurator/
controller/
editor/
gui/
logic/
networking/
rsc/
rsc/gamesettings/
rsc/levels/
rsc/pics/
rsc/properties/
util/
but Java still hates me and throws java.io.FileNotFoundException: rsc/properties/Constants.properties
with a stacktrace of around 100 lines.
但是 Java 仍然讨厌我并抛出java.io.FileNotFoundException: rsc/properties/Constants.properties
大约 100 行的堆栈跟踪。
anybody an idea how to do it? thx & regards
有人知道怎么做吗?谢谢 & 问候
回答by Dan Berindei
If you mark rsc
as a source folder Eclipse will copy its contents to the output directory. Or you could just move rsc
under src
if you want to group the resources in the output.
如果您rsc
将其标记为源文件夹,Eclipse 会将其内容复制到输出目录。或者rsc
,src
如果您想对输出中的资源进行分组,则可以直接移至下方。
Then you can use Class.getResource("/properties/Constants.properties")
or Class.getResource("/rsc/properties/Constants.properties")
and it will work regardless if your app is packaged as a jar or running from the source.
然后您可以使用Class.getResource("/properties/Constants.properties")
orClass.getResource("/rsc/properties/Constants.properties")
并且无论您的应用程序是打包为 jar 还是从源代码运行,它都可以工作。
As a sidenote, the file:
protocol works only with normal files, to access files inside the jar you'd have to use something like new URL("jar:file:app.jar!/rsc/properties/Constants.properties")
.
作为旁注,该file:
协议仅适用于普通文件,要访问 jar 中的文件,您必须使用类似new URL("jar:file:app.jar!/rsc/properties/Constants.properties")
.
回答by Ash
You could try: In the Java Build Path dialog, go to the Libraries tab and select "Add Class Folder...". Select rsc
from the Class Folder Selection dialog. The contents of the rsc
directory will now be included in the executable jar file.
您可以尝试:在 Java Build Path 对话框中,转到 Libraries 选项卡并选择“Add Class Folder...”。选择rsc
从类文件夹选择对话框。该目录的内容rsc
现在将包含在可执行 jar 文件中。
(Note that rsc itself won't be included, so you may have to add a new subdirectory under rsc to make the directory structure in the jar right.)
(请注意, rsc 本身不会被包含在内,因此您可能需要在 rsc 下添加一个新的子目录,以使 jar 中的目录结构正确。)
回答by Valentin Rocher
For this kind of use, I would recommand using Maven, which automatically does that when creating a jar. There's even an Eclipse plugin.
对于这种用途,我建议使用Maven,它会在创建 jar 时自动执行此操作。甚至还有一个 Eclipse 插件。
You could also use Ant.
您也可以使用Ant。
回答by Frank
I do this usually manually, in your case jar -uf foo.jar rsc that includes all your resources. You can write a script or do this in ant
我通常手动执行此操作,在您的情况下 jar -uf foo.jar rsc 包含您的所有资源。您可以编写脚本或在 ant 中执行此操作
回答by rodrigoap
As you say in Edit 2that you have all in one jar, this should work:
正如你在Edit 2 中所说的,你把所有东西都放在一个罐子里,这应该有效:
String path = "rsc/img/img.gif";
URL url = this.getClass().getResource(path);
ImageIcon icon = new ImageIcon(url);