14

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

Hossein Margani
  • 1,056
  • 2
  • 15
  • 35
  • 22
    For those like me, who found this page because tried to find out how to list classes in jar in **command line**, not through Java, the answer is `jar tf yourfile.jar` – Hnatt Mar 14 '13 at 21:03
  • 1
    @Hnatt or any zip reader like 7-zip will work (for Windows), or even `unzip -l yourfile.jar` for *nix. – Matthieu Jul 11 '19 at 16:40

5 Answers5

16

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);
            }
        }
    }
}
YoK
  • 14,329
  • 4
  • 49
  • 67
15

You can view the contents of a JAR file from command prompt by using the following command:

jar tf jar-file

For example:

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  
Xiddoc
  • 3,369
  • 3
  • 11
  • 37
Vishnudev K
  • 2,874
  • 3
  • 27
  • 42
8

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).

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • Thank you very much, and ".class" files is the classes? how can I load .class files as the classes? – Hossein Margani Aug 07 '10 at 06:04
  • So you want to load classes dynamically from a JAR file? That's much more than what you originally asked for. Maybe this question contains some answers: http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime – Jesper Aug 07 '10 at 06:10
4

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

kungfooman
  • 4,473
  • 1
  • 44
  • 33
  • This works really well if you don't have the `jar` command on your station and don't want to install it just for this one check – Mor Paz Jul 23 '18 at 07:31
0

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.

Matthieu
  • 2,736
  • 4
  • 57
  • 87