Listing classes in a jar file

与世无争的帅哥 提交于 2019-12-09 10:06:52

问题


How can I dynamically load a jar file and list classes which is in it?


回答1:


Have a look at the classes in the package java.util.jar. You can find examples of how to list the files inside the JAR on the web, here's an example. (Also note the links at the bottom of that page, there are many more examples that show you how to work with JAR files).




回答2:


Here is code for listing classes in jar:

import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarList {
    public static void main(String args[]) throws IOException {

        if (args.length > 0) {
            JarFile jarFile = new JarFile(args[0]);
            Enumeration allEntries = jarFile.entries();
            while (allEntries.hasMoreElements()) {
                JarEntry entry = (JarEntry) allEntries.nextElement();
                String name = entry.getName();
                System.out.println(name);
            }
        }
    }
}



回答3:


Viewing the Contents of a JAR File

For viewing the contents of a JAR file from command prompt use

jar tf jar-file

eg:-

jar tf TicTacToe.jar


META-INF/MANIFEST.MF  
TicTacToe.class  
audio/  
audio/beep.au  
audio/ding.au  
audio/return.au  
audio/yahoo1.au  
audio/yahoo2.au  
images/  
images/cross.gif  
images/not.gif  



回答4:


Fast way: just open the .jar as .zip e.g. in 7-Zip and look for the directory-names.




回答5:


Here is a version that scans a given jar for all non-abstract classes extending a particular class:

try (JarFile jf = new JarFile("/path/to/file.jar")) {
    for (Enumeration<JarEntry> en = jf.entries(); en.hasMoreElements(); ) {
        JarEntry e = en.nextElement();
        String name = e.getName();
        // Check for package or sub-package (you can change the test for *exact* package here)
        if (name.startsWith("my/specific/package/") && name.endsWith(".class")) {
            // Strip out ".class" and reformat path to package name
            String javaName = name.substring(0, name.lastIndexOf('.')).replace('/', '.');
            System.out.print("Checking "+javaName+" ... ");
            Class<?> cls;
            try {
                cls = Class.forName(javaName);
            } catch (ClassNotFoundException ex)  { // E.g. internal classes, ...
                continue;
            }
            if ((cls.getModifiers() & Modifier.ABSTRACT) != 0) { // Only instanciable classes
                System.out.println("(abstract)");
                continue;
            }
            if (!TheSuper.class.isAssignableFrom(cls)) { // Only subclasses of "TheSuper" class
                System.out.println("(not TheSuper)");
                continue;
            }
            // Found!
            System.out.println("OK");
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

You can use that code directly when you know where are your jars. To get that information, refer to this other question, as going through classpath has changed since Java 9 and the introduction of modules.



来源:https://stackoverflow.com/questions/3429275/listing-classes-in-a-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!