如何使用通配符名称发现Java jar中的资源?

时间:2023-01-17 18:01:31

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

我想发现我的ClassLoader使用通配符模式知道的所有xml文件。有没有办法做到这一点?

5 个解决方案

#1


5  

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.

它需要一些小技巧,但这是一个相关的博客条目。首先找出罐子的URL,然后打开罐子并扫描其内容。我想你会通过查找`/META-INF/MANIFEST.MF'来发现所有罐子的URL。目录将是另一回事。

#2


7  

A Spring ApplicationContext can do this trivially:

Spring ApplicationContext可以做到这一点:

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

See ResourcePatternResolver#getResources, or ApplicationContext.

请参阅ResourcePatternResolver#getResources或ApplicationContext。

#3


6  

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

put the snippet in your pom.xml

把代码段放在你的pom.xml中

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

#4


1  

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

JAR文件只是另一个ZIP文件,对吧?

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

所以我想你可以使用http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html来迭代jar文件

I'm thinking something like:

我想的是:

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

ZipSearcher searcher = new ZipSearcher(new ZipInputStream(new FileInputStream(“my.jar”)));列出xmlFilenames = searcher.search(new RegexFilenameFilter(“。xml $”));

Cheers. Keith.

#5


-4  

Well, it is not from within Java, but

好吧,它不是来自Java内部,而是

jar -tvf jarname | grep xml$

jar -tvf jarname | grep xml $

will show you all the XMLs in the jar.

将显示jar中的所有XML。

#1


5  

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.

它需要一些小技巧,但这是一个相关的博客条目。首先找出罐子的URL,然后打开罐子并扫描其内容。我想你会通过查找`/META-INF/MANIFEST.MF'来发现所有罐子的URL。目录将是另一回事。

#2


7  

A Spring ApplicationContext can do this trivially:

Spring ApplicationContext可以做到这一点:

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

See ResourcePatternResolver#getResources, or ApplicationContext.

请参阅ResourcePatternResolver#getResources或ApplicationContext。

#3


6  

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

put the snippet in your pom.xml

把代码段放在你的pom.xml中

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

#4


1  

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

JAR文件只是另一个ZIP文件,对吧?

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

所以我想你可以使用http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html来迭代jar文件

I'm thinking something like:

我想的是:

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

ZipSearcher searcher = new ZipSearcher(new ZipInputStream(new FileInputStream(“my.jar”)));列出xmlFilenames = searcher.search(new RegexFilenameFilter(“。xml $”));

Cheers. Keith.

#5


-4  

Well, it is not from within Java, but

好吧,它不是来自Java内部,而是

jar -tvf jarname | grep xml$

jar -tvf jarname | grep xml $

will show you all the XMLs in the jar.

将显示jar中的所有XML。