How to load a resource from an embedded JAR file

I am trying to load a resource that is contained in an embedded JAR file. In fact, the project is deployed to JBoss using an EAR file with the following structure:

deploy.ear
|
|-> project.sar
    |
    |-> sub_project.jar
    |   |
    |   |-> settings.xml
    |
    |-> com/path/project/
        |
        |-> main.class

Of main.javaI would like to get InputStreamfor settings.xml. What is the right way to do this?

My real understanding is that the following code should work, but it returns null:

this.getClass().getResourceAsStream("settings.xml");

Update

After some trial and error, the following operators 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");
+5
source share
1 answer

This could be a good resource: http://one-jar.sourceforge.net/version-0.95/

, JAR ClassLoader, JAR, , . StreamClassLoader

, ClassLoader, , getResourceAsStream(...)

+2

All Articles