The uber-fast, ultra-lightweight classpath and module scanner for JVM languages.
npx @tessl/cli install tessl/maven-io-github-classgraph--classgraph@4.8.0ClassGraph is an uber-fast parallelized classpath scanner and module scanner for Java, Scala, Kotlin and other JVM languages. It scans classfiles by parsing the classfile binary format directly rather than using reflection, making it extremely fast and capable of working without loading or initializing classes.
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.8.180</version>
</dependency>import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import io.github.classgraph.ClassInfoList;
// Simple scan with automatic resource cleanup
try (ScanResult scanResult = new ClassGraph().scan()) {
// Get all classes in a specific package
ClassInfoList classes = scanResult.getPackageInfo("com.example").getClassInfo();
// Find all classes with a specific annotation
ClassInfoList annotatedClasses = scanResult.getClassesWithAnnotation("javax.persistence.Entity");
// Find all subclasses of a specific class
ClassInfoList subclasses = scanResult.getSubclasses("java.util.AbstractList");
}import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import io.github.classgraph.ClassInfo;
import io.github.classgraph.ClassInfoList;
import io.github.classgraph.AnnotationInfo;
import io.github.classgraph.AnnotationParameterValue;
import java.util.List;
String pkg = "com.example";
String routeAnnotation = pkg + ".Route";
try (ScanResult scanResult = new ClassGraph()
.verbose() // Enable verbose logging
.enableAllInfo() // Scan classes, methods, fields, annotations
.acceptPackages(pkg) // Limit scan to specific package
.scan()) { // Perform the scan
// Find classes with specific annotation and extract parameter values
for (ClassInfo routeClassInfo : scanResult.getClassesWithAnnotation(routeAnnotation)) {
AnnotationInfo routeAnnotationInfo = routeClassInfo.getAnnotationInfo(routeAnnotation);
List<AnnotationParameterValue> routeParamVals = routeAnnotationInfo.getParameterValues();
String route = (String) routeParamVals.get(0).getValue();
System.out.println(routeClassInfo.getName() + " is annotated with route " + route);
}
}ClassGraph is built around several key components that work together to provide comprehensive classpath scanning capabilities:
The design emphasizes separation of concerns: configuration is isolated in ClassGraph, scanning logic in Scanner, querying in ScanResult, and metadata representation in the Info classes. This modular approach enables efficient resource management, parallel processing, and extensibility for different classpath element types.
ClassGraph uses method chaining for fluent configuration:
ClassGraph classGraph = new ClassGraph()
.verbose() // Enable logging
.enableAllInfo() // Scan everything
.acceptPackages("com.example") // Package filtering
.rejectClasses("com.example.Legacy*") // Class filtering
.enableSystemJarsAndModules() // Include system components
.initializeLoadedClasses(); // Auto-initialize on loadScanResult implements Closeable and should be used in try-with-resources:
try (ScanResult scanResult = new ClassGraph().scan()) {
// Use scan results here
ClassInfoList classes = scanResult.getAllClasses();
} // Automatically closes and cleans up resourcesGeneric methods provide compile-time type safety:
import java.util.List;
// Load and cast classes safely
List<Class<? extends Service>> serviceClasses =
scanResult.getClassesImplementing(Service.class)
.loadClasses(Service.class);
// Get specific annotation with type safety
MyAnnotation annotation = classInfo.getAnnotationInfo(MyAnnotation.class)
.loadClassAndInstantiate();| Feature | Description |
|---|---|
| Zero Dependencies | No external runtime dependencies required |
| JDK 7+ Compatible | Works on JDK 7-21+ with full backwards compatibility |
| Module System Ready | Complete JPMS support while remaining classpath compatible |
| Thread Safe | All operations are thread-safe and optimized for concurrent use |
| Memory Efficient | Lazy loading with automatic resource cleanup |
| Comprehensive Filtering | Package, class, JAR, and module-level filtering |
| Change Detection | Monitor and respond to classpath modifications |
| Serialization Support | JSON serialization for caching and persistence |
ClassGraph enables powerful metaprogramming capabilities for JVM applications by providing complete introspection of class relationships and metadata without the overhead of traditional reflection-based approaches.