Eclipse:导出到 .jar 并包含资源文件 (ANT)

发布于 2024-08-17 19:55:58 字数 1247 浏览 5 评论 0原文

我们在 Eclipse 中的项目大约显示以下文件夹:

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

在我们的项目中,我们想使用 File > 。出口...>>可执行 JAR

工作正常,但有一些例外:如果我们想运行 application.jar,我们仍然需要将文件夹 rsc/ 复制到与 application.jar 相同的位置。

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

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

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

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

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


编辑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/

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


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

Our project in eclipse approximately shows the following folders:

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

In our project, we would like to use 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.

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);

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

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: 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: 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/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃→可爱长大的 2024-08-24 19:55:58

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

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

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

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.

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.

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

叶落知秋 2024-08-24 19:55:58

您可以尝试:在“Java 构建路径”对话框中,转到“库”选项卡并选择“添加类文件夹...”。从类文件夹选择对话框中选择rscrsc 目录的内容现在将包含在可执行 jar 文件中。

(请注意,rsc 本身不会被包含在内,因此您可能需要在 rsc 下添加一个新的子目录以使 jar 中的目录结构正确。)

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.

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

无戏配角 2024-08-24 19:55:58

对于这种用途,我建议使用 Maven,它在创建 jar 时会自动执行此操作。甚至还有Eclipse 插件

您还可以使用 Ant

For this kind of use, I would recommand using Maven, which automatically does that when creating a jar. There's even an Eclipse plugin.

You could also use Ant.

洒一地阳光 2024-08-24 19:55:58

对于你的情况,我通常手动执行此操作
jar -uf foo.jar rsc
其中包括您的所有资源。
您可以编写脚本或在 ant 中执行此操作

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

妄断弥空 2024-08-24 19:55:58

正如您在 编辑 2 中所说,您将所有内容都放在一个罐子中,这应该可行:

String path = "rsc/img/img.gif";
URL url = this.getClass().getResource(path);
ImageIcon icon = new ImageIcon(url);

As you say in Edit 2 that you have all in one jar, this should work:

String path = "rsc/img/img.gif";
URL url = this.getClass().getResource(path);
ImageIcon icon = new ImageIcon(url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文