Explain Codes LogoExplain Codes Logo

How to get names of classes inside a jar file?

java
prompt-engineering
reflection
class-loading
Anton ShumikhinbyAnton Shumikhin·Feb 18, 2025
TLDR

To extract class names from a JAR file, use this short and crisp Java snippet:

import java.util.jar.*; import java.io.IOException; public class JarClassLister { public static void main(String[] args) throws IOException { try (JarFile jar = new JarFile("yourfile.jar")) { jar.stream() .filter(e -> e.getName().endsWith(".class")) .map(e -> e.getName().replace('/', '.').replace(".class", "")) .forEach(System.out::println); } } }

Simply substitute "yourfile.jar" with your JAR file path. This will directly output the class names.

Manual and library-assisted techniques

Several methods are accessible for listing class names in JAR files, apart from the code snippet provided above.

Peeking into a JAR using Java's jar tool

For a quick non-programmatic peek:

jar tvf yourfile.jar

This command prints a list of the JAR's contents, including class names. The output also includes directories, demonstrating the package structure.

Custom method using ZipInputStream

Sometimes, embedding this functionality directly into your Java application may be necessary:

import java.util.zip.*; import java.io.InputStream; try (InputStream is = new FileInputStream("yourfile.jar"); ZipInputStream zis = new ZipInputStream(is)) { for (ZipEntry entry; (entry = zis.getNextEntry()) != null; ) { String fileName = entry.getName(); if (fileName.endsWith(".class")) { // Do you even Java, bro? System.out.println(fileName.replace('/', '.').replace(".class", "")); } } }

Utilising third-party libraries

Guava's ClassPath

A handy library that simplifies things drastically:

import com.google.common.reflect.ClassPath; ClassPath cp = ClassPath.from(Thread.currentThread().getContextClassLoader()); cp.getTopLevelClasses().stream() .forEach(classInfo -> System.out.println(classInfo.getName()));

Note: Including Guava in your project's dependencies is a prerequisite.

Reflections library

This library offers additional flexibility:

import org.reflections.Reflections; import org.reflections.scanners.Scanners; import org.reflections.util.ConfigurationBuilder; Reflections reflections = new Reflections(new ConfigurationBuilder() .forPackages("<package>") // Replace "<package>" with your package name .setScanners(Scanners.TypesAnnotated)); reflections.getTypesAnnotatedWith(MyAnnotation.class).stream() .forEach(System.out::println);

Command-line autocomplete magic

The terminal provides an autocomplete feature that works splendidly with JAR files:

Type java -cp yourfile.jar and press <Tab> – voila! You'll get a hint including class names. Note that this trick requires the JAR to have a defined main class.

Proactive measures: Handling potential pitfalls

While these techniques cover various scenarios, it's always good to be prepared for pitfalls:

  1. A misplaced directory or incorrect file name can result in errors, make sure the file path is accurate.
  2. Class-loading scenarios: If you're using Guava or Reflections, verify the classes from the JAR are loaded in your class loader.
  3. When dealing with large JAR files, consider memory management while choosing the method of extraction. Streaming contents with ZipInputStream or JarFile can be beneficial.