代码之家  ›  专栏  ›  技术社区  ›  Jeff

如何在带有通配符名称的Java jar中发现资源?

  •  8
  • Jeff  · 技术社区  · 17 年前

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

    5 回复  |  直到 17 年前
        1
  •  6
  •   flicken    17 年前

    ApplicationContext 我们可以做到这一点:

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

    看见 ResourcePatternResolver#getResources ApplicationContext

        2
  •  6
  •   Serhat    13 年前
     List<URL> resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.sample"), new ResourceNameFilter("A*.xml"));
    

    <dependency>
        <groupId>net.sf.corn</groupId>
        <artifactId>corn-cps</artifactId>
        <version>1.0.1</version>
    </dependency>
    
        3
  •  5
  •   sblundy    17 年前

    这需要一些技巧,但这里有一个相关的 blog entry

        4
  •  1
  •   corlettk    17 年前

    所以我想您可以使用 http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html

    我的想法是:

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

        5
  •  -5
  •   john_science    13 年前

    它不是来自Java内部,而是

    jar -tvf jarname | grep xml$

    将显示jar中的所有xml。