How to list the files inside a JAR file?

前端 未结 16 2239
温柔的废话
温柔的废话 2020-11-22 00:40

I have this code which reads all the files from a directory.

    File textFolder = new File(\"text_directory\");

    File [] texFiles = textFolder.listFiles         


        
16条回答
  •  遇见更好的自我
    2020-11-22 01:17

    The most robust mechanism for listing all resources in the classpath is currently to use this pattern with ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author of ClassGraph.)

    How to know the name of the JAR file where my main class lives?

    URI mainClasspathElementURI;
    try (ScanResult scanResult = new ClassGraph().whitelistPackages("x.y.z")
            .enableClassInfo().scan()) {
        mainClasspathElementURI =
                scanResult.getClassInfo("x.y.z.MainClass").getClasspathElementURI();
    }
    

    How can I read the contents of a directory in a similar fashion within a JAR file?

    List classpathElementResourcePaths;
    try (ScanResult scanResult = new ClassGraph().overrideClasspath(mainClasspathElementURI)
            .scan()) {
        classpathElementResourcePaths = scanResult.getAllResources().getPaths();
    }
    

    There are lots of other ways to deal with resources too.

提交回复
热议问题