如何在具有通配符名称的Java jar中发现资源?
时间:2020-03-06 14:43:10 来源:igfitidea点击:
我想使用通配符模式发现我的ClassLoader知道的所有xml文件。有什么办法吗?
解决方案
嗯,它不是来自Java内部的,而是
jar -tvf jarname | grep xml $
将向我们显示jar中的所有XML。
这需要一些技巧,但这是一个相关的博客条目。我们首先要找出罐子的URL,然后打开罐子并扫描其内容。我认为我们可以通过查找`/META-INF/MANIFEST.MF'来发现所有jar的URL。目录将是另一回事。
JAR文件只是另一个ZIP文件,对吗?
因此,我想我们可以使用http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html迭代jar文件
我在想类似的东西:
ZipSearcher searcher = new ZipSearcher(new ZipInputStream(new FileInputStream(" my.jar")));; 列出xmlFilenames = searcher.search(new RegexFilenameFilter("。xml $"));
干杯。基思
SpringApplicationContext
可以简单地做到这一点:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationConext.xml"); Resource[] xmlResources = context.getResources("classpath:/**/*.xml");
请参阅ResourcePatternResolver#getResources或者ApplicationContext。