Listing classes in a jar file

前端 未结 5 1076
感动是毒
感动是毒 2021-02-14 01:20

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

5条回答
  •  不要未来只要你来
    2021-02-14 02:03

    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);
                }
            }
        }
    }
    

提交回复
热议问题