Dough
Dough

Reputation: 455

Accessing resources within JAR

I'm creating a Java application that uses a Maven profile and a JAR build.

I have some XML files in this folder : project/src/main/resources/XML/*.xml

The src/main/resources folder is a source folder in my build path so I have my project.jar/XML/*.xml built in the JAR binary.

I'd like to be able to find and list those files "/XML/*.xml" in JAR execution. Is my structure good for this ?

I have no problem running with the Maven profile, I just can't find the good way to find and list those files.

Upvotes: 1

Views: 1818

Answers (3)

Ian Roberts
Ian Roberts

Reputation: 122414

There isn't really a good way. If you don't mind the dependency on spring-core then they have a nice PathMatchingResourcePatternResolver that abstracts away from the differences between JARs and directories.

PathMatchingResourcePatternResolver resolver =
   new PathMatchingResourcePatternResolver(MyClass.class.getClassLoader());
Resource[] xmls = resolver.getResources("classpath*:XML/*.xml");

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136112

This code demonstrates how to find out the path of a jar on your app's classpath and list the contents of a folder.

// you need to get of a URL of any class in your jar
URL url = Appender.class.getProtectionDomain().getCodeSource()
           .getLocation();
File file = new File(url.getFile());
System.out.println(file.getCanonicalPath());
JarFile jar = new JarFile(file);
Enumeration<JarEntry> i = jar.entries();
while (i.hasMoreElements()) {
    JarEntry e = i.nextElement();
    if (e.getName().startsWith("META-INF/")) {
        System.out.println(e.getName());
    }
}
jar.close();

output

jar:file:/C:/temp/.repository/log4j/log4j/1.2.17/log4j-1.2.17.jar!/org/apache/log4j/Appender.class
C:\temp\.repository\log4j\log4j\1.2.17\log4j-1.2.17.jar
META-INF/MANIFEST.MF
META-INF/
META-INF/LICENSE
META-INF/NOTICE
META-INF/maven/
META-INF/maven/log4j/
META-INF/maven/log4j/log4j/
META-INF/maven/log4j/log4j/pom.properties
META-INF/maven/log4j/log4j/pom.xml

Upvotes: 0

SkyWalker
SkyWalker

Reputation: 14317

If you want to list the contents of your jar file, you could just simply run:

jar tf ./path/to/your/file.jar

or

jar tf ./path/to/your/file.jar | grep XML

Now from code (I believe this is not the only way to do it) and you have to replace Main with whatever class you are in that is within the jar:

              List<String> myJarXmlFiles = new ArrayList<String>();
    try {
                       // replace Main here   
        CodeSource src = Main.class.getProtectionDomain().getCodeSource();
        if (src != null) {
            URL jar = src.getLocation();
            ZipInputStream zip = new ZipInputStream(jar.openStream());
            ZipEntry entry = null;
            while ((entry = zip.getNextEntry()) != null) {
                if (entry.getName().contains("XML/")
                        && entry.getName().endsWith(".xml")) {
                    myJarXmlFiles.add(entry.getName());
                }
            }
        } else {
            // failed
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 0

Related Questions