Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

Most Java libraries are available as JAR files. In this tutorial, we'll address how to get names of classes inside a given JAR file from the command line and from a Java program.

Then, we'll look at a Java program example of loading the classes from a given JAR file at runtime.

2. Example JAR File

In this tutorial, we'll take the stripe-0.0.1-SNAPSHOT.jar file as an example to address how to get the class names in the JAR file:

3. Using the jar Command

JDK ships with a jar command. We can use this command with the t and f options to list the content of a JAR file:

$ jar tf stripe-0.0.1-SNAPSHOT.jar 
META-INF/
META-INF/MANIFEST.MF
...
templates/result.html
templates/checkout.html
application.properties
com/baeldung/stripe/StripeApplication.class
com/baeldung/stripe/ChargeRequest.class
com/baeldung/stripe/StripeService.class
com/baeldung/stripe/ChargeRequest$Currency.class
...

Since we're only interested in the *.class files in the archive, we can filter the output using the grep command:

$ jar tf stripe-0.0.1-SNAPSHOT.jar | grep '\.class$'
com/baeldung/stripe/StripeApplication.class
com/baeldung/stripe/ChargeRequest.class
com/baeldung/stripe/StripeService.class
com/baeldung/stripe/ChargeRequest$Currency.class
com/baeldung/stripe/ChargeController.class
com/baeldung/stripe/CheckoutController.class

This gives us a list of class files inside the JAR file.

4. Getting Class Names of a JAR File in Java

Using the jar command to print the class names from a JAR file is pretty straightforward. However, sometimes we want to load some classes from a JAR file in our Java program. In this case, the command-line output isn't enough.

To achieve our objective, we need to scan the JAR file from a Java program and get the class names.

Let's have a look at how to extract class names from our example JAR file using the JarFile and JarEntry classes:

public static Set<String> getClassNamesFromJarFile(File givenFile) throws IOException {
    Set<String> classNames = new HashSet<>();
    try (JarFile jarFile = new JarFile(givenFile)) {
        Enumeration<JarEntry> e = jarFile.entries();
        while (e.hasMoreElements()) {
            JarEntry jarEntry = e.nextElement();
            if (jarEntry.getName().endsWith(".class")) {
                String className = jarEntry.getName()
                  .replace("/", ".")
                  .replace(".class", "");
                classNames.add(className);
            }
        }
        return classNames;
    }
}

Now, let's take a closer look at the code in the method above and understand how it works:

  • try (JarFile jarFile = new JarFile(givenFile)) – Here, we used a try-with-resources statement to get the jarFile from the given File object
  • if (jarEntry.getName().endsWith(“.class”)){…} – We take each class jarEntry, and change the path of the class file into the qualified class name, for example change “package1/package2/SomeType.class” into “package1.package2.SomeType”

Let's verify if the method can extract the class names from our example JAR file through a unit test method:

private static final String JAR_PATH = "example-jar/stripe-0.0.1-SNAPSHOT.jar";
private static final Set<String> EXPECTED_CLASS_NAMES = Sets.newHashSet(
  "com.baeldung.stripe.StripeApplication",
  "com.baeldung.stripe.ChargeRequest",
  "com.baeldung.stripe.StripeService",
  "com.baeldung.stripe.ChargeRequest$Currency",
  "com.baeldung.stripe.ChargeController",
  "com.baeldung.stripe.CheckoutController");

@Test
public void givenJarFilePath_whenLoadClassNames_thenGetClassNames() throws IOException, URISyntaxException {
    File jarFile = new File(
      Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI());

    Set<String> classNames = GetClassNamesFromJar.getClassNamesFromJarFile(jarFile);

    Assert.assertEquals(EXPECTED_CLASS_NAMES, classNames);
}

5. Getting Classes From a JAR File in Java

We've seen how to get the class names from a JAR file. Sometimes, we want to load some classes from a JAR file at runtime dynamically.

In this case, we can first get the class names from the given JAR file using our getClassNamesFromJarFile method.

Next, we can create a ClassLoader to load required classes by name:

public static Set<Class> getClassesFromJarFile(File jarFile) throws IOException, ClassNotFoundException {
    Set<String> classNames = getClassNamesFromJarFile(jarFile);
    Set<Class> classes = new HashSet<>(classNames.size());
    try (URLClassLoader cl = URLClassLoader.newInstance(
           new URL[] { new URL("jar:file:" + jarFile + "!/") })) {
        for (String name : classNames) {
            Class clazz = cl.loadClass(name); // Load the class by its name
            classes.add(clazz);
        }
    }
    return classes;
}

In the method above, we created a URLClassLoader object to load the classes. The implementation is pretty straightforward.

However, it's probably worth explaining the syntax for the JAR URL a little bit. A valid JAR URL contains three parts: “jar: + [the location of the JAR file] + !/”. 

The terminating “!/” indicates that the JAR URL refers to an entire JAR file.  Let's see a few JAR URL examples:

jar:http://www.example.com/some_jar_file.jar!/
jar:file:/local/path/to/some_jar_file.jar!/
jar:file:/C:/windows/path/to/some_jar_file.jar!/

In our getClassesFromJarFile method, the JAR file is located on the local filesystem, therefore, the prefix of the URL is “file:“.

Now, let's write a test method to verify if our method can get all expected Class objects:

@Test
public void givenJarFilePath_whenLoadClass_thenGetClassObjects()
  throws IOException, ClassNotFoundException, URISyntaxException {
    File jarFile
      = new File(Objects.requireNonNull(getClass().getClassLoader().getResource(JAR_PATH)).toURI());
    Set<Class> classes = GetClassNamesFromJar.getClassesFromJarFile(jarFile);
    Set<String> names = classes.stream().map(Class::getName).collect(Collectors.toSet());
    Assert.assertEquals(EXPECTED_CLASS_NAMES, names);
}

Once we have the required Class objects, we can use Java reflection to create instances of classes and invoke methods.

6. Conclusion

In this article, we've learned two different approaches to get class names from a given JAR file.

The jar command can print the class names. It's pretty handy if we need to check whether a JAR file contains a given class. However, if we need to get the class names from a running Java program, JarFile and JarEntry can help us achieve that.

At last, we've also seen an example of a Java program to load classes from a JAR file at runtime.

As always, the full source code of the article is available over on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!