Question

I want to discover all xml files that my ClassLoader is aware of using a wildcard pattern. Is there any way to do this?

Was it helpful?

Solution

It requires a little trickery, but here's an relevant blog entry. You first figure out the URLs of the jars, then open the jar and scan its contents. I think you would discover the URLs of all jars by looking for `/META-INF/MANIFEST.MF'. Directories would be another matter.

OTHER TIPS

A Spring ApplicationContext can do this trivially:

 ApplicationContext context = new ClassPathXmlApplicationContext("applicationConext.xml");
 Resource[] xmlResources = context.getResources("classpath:/**/*.xml");

See ResourcePatternResolver#getResources, or ApplicationContext.

 List<URL> resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.sample"), new ResourceNameFilter("A*.xml"));

put the snippet in your pom.xml

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>

A JAR-file is just another ZIP-file, right?

So I suppose you could iterate the jar-files using http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html

I'm thinking something like:

ZipSearcher searcher = new ZipSearcher(new ZipInputStream(new FileInputStream("my.jar"))); List xmlFilenames = searcher.search(new RegexFilenameFilter(".xml$"));

Cheers. Keith.

Well, it is not from within Java, but

jar -tvf jarname | grep xml$

will show you all the XMLs in the jar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top