How to get names of classes inside a jar file? How to get names of classes inside a jar file? java java

How to get names of classes inside a jar file?


You can use Java jar tool. List the content of jar file in a txt file and you can see all the classes in the jar.

jar tvf jarfile.jar

-t list table of contents for archive

-v generate verbose output on standard output

-f specify archive file name


Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the .class files, and then determine which Java class each .class file represents; or (b) you can use a library that does this for you.

Option (a): Scanning JAR files manually

In this option, we'll fill classNames with the list of all Java classes contained inside a jar file at /path/to/jar/file.jar.

List<String> classNames = new ArrayList<String>();ZipInputStream zip = new ZipInputStream(new FileInputStream("/path/to/jar/file.jar"));for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {    if (!entry.isDirectory() && entry.getName().endsWith(".class")) {        // This ZipEntry represents a class. Now, what class does it represent?        String className = entry.getName().replace('/', '.'); // including ".class"        classNames.add(className.substring(0, className.length() - ".class".length()));    }}

Option (b): Using specialized reflections libraries

Guava

Guava has had ClassPath since at least 14.0, which I have used and liked. One nice thing about ClassPath is that it doesn't load the classes it finds, which is important when you're scanning for a large number of classes.

ClassPath cp=ClassPath.from(Thread.currentThread().getContextClassLoader());for(ClassPath.ClassInfo info : cp.getTopLevelClassesRecurusive("my.package.name")) {    // Do stuff with classes here...}

Reflections

I haven't personally used the Reflections library, but it seems well-liked. Some great examples are provided on the website like this quick way to load all the classes in a package provided by any JAR file, which may also be useful for your application.

Reflections reflections = new Reflections("my.project.prefix");Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);


Maybe you are looking for jar command to get the list of classes in terminal,

$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar META-INF/META-INF/MANIFEST.MForg/org/apache/org/apache/spark/org/apache/spark/unused/org/apache/spark/unused/UnusedStubClass.classMETA-INF/maven/META-INF/maven/org.spark-project.spark/META-INF/maven/org.spark-project.spark/unused/META-INF/maven/org.spark-project.spark/unused/pom.xmlMETA-INF/maven/org.spark-project.spark/unused/pom.propertiesMETA-INF/NOTICE

where,

-t  list table of contents for archive-f  specify archive file name

Or, just grep above result to see .classes only

$ jar tf ~/.m2/repository/org/apache/spark/spark-assembly/1.2.0-SNAPSHOT/spark-assembly-1.2.0-SNAPSHOT-hadoop1.0.4.jar | grep .classorg/apache/spark/unused/UnusedStubClass.class

To see number of classes,

jar tvf launcher/target/usergrid-launcher-1.0-SNAPSHOT.jar | grep .class | wc -l61079