Class ClassPath
java.lang.Object
com.google.common.reflect.ClassPath
Scans the source of a Limitations of
ClassLoader and finds all loadable classes and resources.
Prefer alternatives to classpath scanning
Runtime classpath scanning is expensive because it traverses the entire transitive classpath, which can cause performance issues such as startup latency regressions. (It also does not support Android.) Before scanning the classpath at runtime, consider whether your use case can be solved with compile-time or static mechanisms:
- Compile-time service registration / annotation processors (e.g.,
@AutoService) - Dependency injection (e.g., Dagger, Guice)
- Explicit static registries or configuration
- Compile-time classpath scanning, such as that of ClassGraph, but compile-time scanning is likewise costly, so prefer the other options when possible
Limitations of ClassPath (for those who do need classpath scanning)
- It looks only for files and JARs in URLs available from
URLClassLoaderinstances or the system class loader. This means it does not look for classes in the module path or in other environments. If you need to scan one of those locations, consider ClassGraph. - Like all of Guava, it is less battle-tested under Windows and other non-Linux systems. We have gotten a report of a specific bug under Windows.
- It returns only one resource for a given path, even if resources with that path appear in multiple jars or directories.
- It uses heuristics to guess at which classes are nested and which are not.
ClassPath and symlinks
In the case of directory classloaders, symlinks are supported but cycles are not traversed. This guarantees discovery of each unique loadable resource. However, not all possible aliases for resources on cyclic paths will be listed.
- Since:
- 14.0
- Author:
- Ben Yu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a class that can be loaded throughClassPath.ClassInfo.load().static classRepresents a class path resource that can be either a class file or any other resource file loadable from the class path. -
Method Summary
Modifier and TypeMethodDescriptionstatic ClassPathfrom(ClassLoader classloader) Returns aClassPathrepresenting all classes and resources loadable fromclassloaderand its ancestor class loaders.Returns all classes loadable from the current class path.Returns all resources loadable from the current class path, including the class files of all loadable classes but excluding the "META-INF/MANIFEST.MF" file.Returns all top level classes loadable from the current class path.getTopLevelClasses(String packageName) Returns all top level classes whose package name ispackageName.getTopLevelClassesRecursive(String packageName) Returns all top level classes whose package name ispackageNameor starts withpackageNamefollowed by a '.'.
-
Method Details
-
from
Returns aClassPathrepresenting all classes and resources loadable fromclassloaderand its ancestor class loaders.Warning:
ClassPathcan find classes and resources only from:URLClassLoaderinstances'file:URLs- the system class loader. To search the
system class loader even when it is not a
URLClassLoader(as in Java 9),ClassPathsearches the files from thejava.class.pathsystem property.
- Throws:
IOException- if the attempt to read class path resources (jar files or directories) failed.
-
getResources
Returns all resources loadable from the current class path, including the class files of all loadable classes but excluding the "META-INF/MANIFEST.MF" file. -
getAllClasses
Returns all classes loadable from the current class path.- Since:
- 16.0
-
getTopLevelClasses
Returns all top level classes loadable from the current class path. Note that "top-level-ness" is determined heuristically by class name (seeClassPath.ClassInfo.isTopLevel()). -
getTopLevelClasses
Returns all top level classes whose package name ispackageName. -
getTopLevelClassesRecursive
Returns all top level classes whose package name ispackageNameor starts withpackageNamefollowed by a '.'.
-