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

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

问题描述

我知道还有其他一些针对同样问题的问题,但事实是这些解决方案对我来说无效。
我有一个工具,应该读取我想要的文件,并作为资源打包,并依赖于其他项目(我使用的是Eclipse Helios),我希望将其作为jar而不是单个类。 / p>

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



$ pre> String访问我的资源path = MyClass.class.getClassLoader()。getResource(resources /+ name +.SOURCE)。getPath();
System.out.println(path);
文件文件= new File(MyClass.class.getClassLoader()。getResource(resources /+ name +.SOURCE)。toURI());
defaultSource = readSOURCEFile(file);

如果且仅当我将资源文件夹放在输出路径下(编译源)NOT如果我将其放在我的src文件夹中。



当我打包项目时,根文件夹或classes文件夹中的资源根本不打包。如果我有资源在我的src文件夹,它被包装在src / resources下。



我使用Export - Runnable Jar,我做错了什么?
如果我尝试使用Export - Jar,我可以正确打包我的资源和资源,但我有设置主类和无法执行jar的问题。清单是正确的,类在这里: - (。



第二个问题:在打包的jar中我得到错误:URI不是分层的(在我移动资源之后在jar内手动我可以执行jar)



提前感谢!



工作空间是一个问题?

解决方案

标记资源您的项目的根文件夹作为Eclipse中的源文件夹(右键单击该文件夹,转到构建路径>用作源文件夹),然后阅读如下资源:

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


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.

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

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.

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.

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

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)

Thanks in advance!

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

解决方案

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天全站免登陆