如何正确打包Jar中的资源 [英] How to package resources in Jar properly

查看:29
本文介绍了如何正确打包Jar中的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道还有其他针对同一问题的问题,但问题是这些解决方案对我不起作用.我有一个小工具,可以读取我想要的文件并将其打包为资源,并依赖于其他项目(我使用的是 Eclipse Helios),我希望将其作为 jar 而不是单个类.

I know that there are other questions out there targeting the same issue but the thing is that the solutions don't work for me. I have a little tool that is supposed to read files I want to have and package as resources and relies on other projects (I am using Eclipse Helios) which I'd love to have as jars instead of single classes.

作为 Eclipse 中的应用程序,我可以通过

As an application within Eclipse I can access my resources via

String path = MyClass.class.getClassLoader().getResource("resources/" + name + ".SOURCE").getPath();
System.out.println(path);
File file = new File(MyClass.class.getClassLoader().getResource("resources/" + name + ".SOURCE").toURI());
defaultSource = readSOURCEFile(file);

当且仅当我将资源文件夹放在输出路径(编译源)下时,而不是将其放在我的 src 文件夹中.

if and only if I place the resources folder under the output path (the compiled sources) NOT if I place it in my src folder.

当我打包项目时,根文件夹或classes文件夹中的资源根本没有打包.如果我的 src 文件夹中有资源,它就会打包在 src/resources 下.

When I package the project, resources in the root folder or in the classes folder are not packaged at all. If I have the resources in my src folder it get's packaged under src/resources.

我使用Export - Runnable Jar,我做错了什么?如果我尝试使用 Export - Jar,我可以正确打包我的源和资源,但是我在设置主类时遇到问题并且无法执行 jar.清单是正确的,课程就在那里:-(.

I use Export - Runnable Jar, what am I doing wrong? If I try with Export - Jar, I can package my sources and resources properly but I have issues with setting the main class and can't execute the jar. The Manifest is correct and the class is there :-(.

第二个问题:在打包的 jar 中,我收到错误:URI is not层次化(在我手动移动 jar 中的资源后,我可以执行 jar)

Second problem: In the packaged jar I get the error: URI is not hierarchical (after I moved the resources within the jar manually I could execute the jar)

提前致谢!

我已将源链接到工作区,这是一个问题吗?

I have linked the sources into the workspace is that an issue?

推荐答案

在 Eclipse 中将项目根文件夹下的 resource 文件夹标记为源文件夹"(右键单击该文件夹,转到构建路径">用作源文件夹").然后像这样阅读资源:

Mark the resource folder under your project's root folder as a "Source Folder" in Eclipse (right click on the folder, go to "Build Path" > "Use as source folder"). Then read the resources like this:

InputStream is = MyClass.class.getClassLoader().getResourceAsStream(name + ".SOURCE");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();

这篇关于如何正确打包Jar中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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