Eclipse:导出到 .jar 并包含资源文件 (ANT) [英] Eclipse: export to .jar AND include resource files (ANT)

查看:25
本文介绍了Eclipse:导出到 .jar 并包含资源文件 (ANT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 eclipse 中的项目大致显示了以下文件夹:

Our project in eclipse approximately shows the following folders:

application
    - src
    - JRE System Library [1.6]
    - Referenced Libraries
    - lib
    - rsc

在我们的项目中,我们想使用 File > Export... > Executable JAR

In our project, we would like to use File > Export... > Executable JAR

效果很好,但有一些例外:如果我们想运行 application.jar,我们仍然需要将文件夹 rsc/复制到 application.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.

在 rsc/中,我们有其他文件夹用于分隔不同的部分.基本上,我们使用代码加载这些片段(有点改变,但路径样式是正确的)

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)

strUrl = "file:rsc/properties/Constants.properties";
url = new URL(strUrl);
ImageIcon icon = new ImageIcon(url);

我可以右键单击 rsc/> 构建路径 > 用作源文件夹.

I could rightclick on rsc/ > Build Path > Use as Source Folder.

但这也不起作用,因为 eclipse 不会自动将 rsc 文件夹复制到 bin/-folder 中.然后我们不能再从终端运行它(没有打包到 jar 中)

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)

EDIT:为什么 eclipse 无法正确处理这个问题?为什么嵌套"输出文件夹会出现问题?在构建路径"对话框的源"选项卡中,我可以轻松添加 rsc/-文件夹,并将输出文件夹设置为 bin/rsc/,但它似乎无法放置嵌套文件夹...

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...

EDIT 2:现在我已经能够创建一个 xml 文件来用 ant 构建东西,并且以某种方式设法将 rsc 文件夹直接包含到 jar 中.由于相同的错误,仍然无法运行.我真的需要检查资源路径是否在 JAR 中吗?JAR 的内容现在如下:

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:

META-INF/
META-INF/MANIFEST.MF
configurator/
controller/
editor/
gui/
logic/
networking/
rsc/
rsc/gamesettings/
rsc/levels/
rsc/pics/
rsc/properties/
util/

但 Java 仍然讨厌我并抛出 java.io.FileNotFoundException: rsc/properties/Constants.properties 堆栈跟踪大约 100 行.

but Java still hates me and throws java.io.FileNotFoundException: rsc/properties/Constants.properties with a stacktrace of around 100 lines.

有人知道怎么做吗?谢谢&问候

anybody an idea how to do it? thx & regards

推荐答案

如果您将 rsc 标记为源文件夹,Eclipse 会将其内容复制到输出目录.或者,如果您想对输出中的资源进行分组,您可以将 rsc 移动到 src 下.

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.

然后你可以使用 Class.getResource("/properties/Constants.properties")Class.getResource("/rsc/properties/Constants.properties")无论您的应用是打包为 jar 还是从源代码运行,它都能正常工作.

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.

作为旁注,file: 协议仅适用于普通文件,要访问 jar 中的文件,您必须使用类似 new URL("jar:file:app.jar!/rsc/properties/Constants.properties").

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").

这篇关于Eclipse:导出到 .jar 并包含资源文件 (ANT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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