ClassGraph is an uber-fast parallelized classpath scanner and module scanner for Java, Scala, Kotlin and other JVM languages.
| ClassGraph won a Duke's Choice Award (a recognition of the most useful and/or innovative software in the Java ecosystem) at Oracle Code One 2018, and a Google Open Source Peer Bonus award in 2022. Thanks to all the users who have reported bugs, requested features, offered suggestions, and submitted pull requests to help get ClassGraph to where it is today. |
|---|
| ClassGraph is stable and mature, and has a low bug report rate, despite being used by hundreds of projects. |
|---|
ClassGraph has the ability to "invert" the Java class and/or reflection API, or has the ability to index classes and resources. For example, the Java class and reflection API can tell you the superclass of a given class, or the interfaces implemented by a given class, or can give you the list of annotations on a class; ClassGraph can find all classes that extend a given class (all subclasses of a given class), or all classes that implement a given interface, or all classes that are annotated with a given annotation. The Java API can load the content of a resource file with a specific path in a specific ClassLoader, but ClassGraph can find and load all resources in all classloaders with paths matching a given pattern.
The following code prints the name of all classes in the package com.xyz or its subpackages, anywhere on the classpath or module path, that are annotated with an annotation of the form @com.xyz.Route("/pages/home.html"), along with the annotation parameter value. This is accomplished without loading or initializing any of the scanned classes.
String pkg = "com.xyz";
String routeAnnotation = pkg + ".Route";
try (ScanResult scanResult =
new ClassGraph()
.verbose() // Log to stderr
.enableAllInfo() // Scan classes, methods, fields, annotations
.acceptPackages(pkg) // Scan com.xyz and subpackages (omit to scan all packages)
.scan()) { // Start the scan
for (ClassInfo routeClassInfo : scanResult.getClassesWithAnnotation(routeAnnotation)) {
AnnotationInfo routeAnnotationInfo = routeClassInfo.getAnnotationInfo(routeAnnotation);
List<AnnotationParameterValue> routeParamVals = routeAnnotationInfo.getParameterValues();
// @com.xyz.Route has one required parameter
String route = (String) routeParamVals.get(0).getValue();
System.out.println(routeClassInfo.getName() + " is annotated with route " + route);
}
}
The following code finds all JSON files in META-INF/config in all ClassLoaders or modules, and calls the method readJson(String path, String content) with the path and content of each file.
try (ScanResult scanResult = new ClassGraph().acceptPathsNonRecursive("META-INF/config").scan()) {
scanResult.getResourcesWithExtension("json")
.forEachByteArray((Resource res, byte[] content) -> {
readJson(res.getPath(), new String(content, StandardCharsets.UTF_8));
});
}
See the code examples page for more examples of how to use the ClassGraph API.
ClassGraph provides a number of important capabilities to the JVM ecosystem:
Replace X.Y.Z below with the latest release number. (Alternatively, you could use LATEST in place of X.Y.Z instead if you just want to grab the latest version -- although be aware that that may lead to non-reproducible builds, since the ClassGraph version number could increase at any time. You could use dependency locking to address this.)
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>X.Y.Z</version>
</dependency>
See instructions for use as a module.
The JDK team decided to start enforcing strong encapsulation in JDK 16+. That will means that by default, ClassGraph will not be able to find the classpath of your project, if all of the following are true:
If your ClassGraph code works in JDK versions less than 16 but breaks in JDK 16+ (meaning that ClassGraph can no longer find your classes), you have probably run into this problem.
ClassGraph can use either of the following libraries to silently circumvent all of Java's security mechanisms (visibility/access checks, security manager restrictions, and strong encapsulation), in order to read the classpath from private fields and methods of classloaders.
To clarify, you do only need to use Narcissus or JVM-driver if ClassGraph cannot find the classpath elements from your classloader, due to the enforcement of strong encapsulation, or if it is problematic that you are getting reflection access warnings on the console.
To use one of these libraries:
ClassGraph.CIRCUMVENT_ENCAPSULATION = CircumventEncapsulationMethod.NARCISSUS; before interacting with ClassGraph in any other way (this will load the Narcissus library as ClassGraph's reflection driver).ClassGraph.CIRCUMVENT_ENCAPSULATION = CircumventEncapsulationMethod.JVM_DRIVER; before interacting with ClassGraph in any other way (this will load the JVM-Driver library as ClassGraph's reflection driver).JDK 16's strong encapsulation is just the first step of trying to lock down Java's internals, so further restrictions are possible (e.g. it is likely that setAccessible(true) will fail in future JDK releases, even within a module, and probably the JNI API will be locked down soon, making Narcissus require a commandline flag to work). Therefore, please convince your upstream runtime environment maintainers to expose the full classpath from their classloader using a public method or field, otherwise ClassGraph may stop working for your runtime environment in the future.
You can get pre-built JARs (usable on JRE 7 or newer) from Sonatype.
ClassGraph must be built on JDK 8 or newer (due to the presence of @FunctionalInterface annotations on some interfaces), but is built using -target 1.7 for backwards compatibility with JRE 7.
The following commands will build the most recent version of ClassGraph from git master. The compiled package will then be in the "classgraph/target" directory.
git clone https://github.com/classgraph/classgraph.git
cd classgraph
export JAVA_HOME=/usr/java/default # Or similar -- Maven needs JAVA_HOME
./mvnw -Dmaven.test.skip=true package
This will allow you to build a local SNAPSHOT jar in target/. Alternatively, use ./mvnw -Dmaven.test.skip=true install to build a SNAPSHOT jar and then copy it into your local repository, so that you can use it in your Maven p
$ claude mcp add classgraph \
-- python -m otcore.mcp_server <graph>