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

8 java classpath

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

fli*_*ken 7

Spring ApplicationContext可以做到这一点:

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

请参阅ResourcePatternResolver#getResourcesApplicationContext.


小智 6

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

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

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


sbl*_*ndy 5

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