[英]How to load a resource from an embedded JAR File
I am trying to load a resource that is contained within an embedded JAR file. 我正在尝试加载嵌入的JAR文件中包含的资源。 The project is actually deployed in JBoss using an EAR file with the following structure:
该项目实际上是使用具有以下结构的EAR文件部署在JBoss中的:
deploy.ear
|
|-> project.sar
|
|-> sub_project.jar
| |
| |-> settings.xml
|
|-> com/path/project/
|
|-> main.class
From main.java
I'd like to get a InputStream
for settings.xml
. 从
main.java
我想获得settings.xml
的InputStream
。 What is the correct way to do this? 这样做的正确方法是什么?
My current understanding that the following code should work, but it is returning null
: 我目前的理解是以下代码应该工作,但它返回
null
:
this.getClass().getResourceAsStream("settings.xml");
Update 更新
After some trial and error, the following statements work: 经过一些反复试验后,以下陈述有效:
getClass().getResourceAsStream("/settings.xml");
getClass().getResourceAsStream("/sub_project.jar/settings.xml");
getClass().getClassLoader().getResourceAsStream("/settings.xml");
getClass().getClassLoader().getResourceAsStream("settings.xml");
getClass().getClassLoader().getResourceAsStream("sub_project.jar/settings.xml");
getClass().getClassLoader().getResourceAsStream("/sub_project.jar/settings.xml");
This might be a good resource: http://one-jar.sourceforge.net/version-0.95/ 这可能是一个很好的资源: http : //one-jar.sourceforge.net/version-0.95/
The main idea is that the inner JAR is not loaded by the ClassLoader that loaded the outer JAR automatically, you need to do so manually, eg by using a StreamClassLoader
to load the inner jar 主要思想是内部JAR不是由自动加载外部JAR的ClassLoader加载的,您需要手动执行,例如通过使用
StreamClassLoader
加载内部jar
Only then, from your own ClassLoader you can get that resource using getResourceAsStream(...)
只有这样,从您自己的ClassLoader,您可以使用
getResourceAsStream(...)
获取该资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.