Package com.google.common.reflect
Class ClassPath
java.lang.Object
com.google.common.reflect.ClassPath
Scans the source of a Prefer ClassGraph over
ClassLoader
and finds all loadable classes and resources.
Prefer ClassGraph over
ClassPath
We recommend using ClassGraph
instead of ClassPath
. ClassGraph improves upon ClassPath
in several ways,
including addressing many of its limitations. Limitations of ClassPath
include:
- It looks only for files and JARs in URLs available from
URLClassLoader
instances or the system class loader. This means it does not look for classes in the module path. - It understands only
file:
URLs. This means that it does not understandjrt:/
URLs, among others. - It does not know how to look for classes when running under an Android VM. (ClassGraph does not support this directly, either, but ClassGraph documents how to perform build-time classpath scanning and make the results available to an Android app.)
- Like all of Guava, it is not tested under Windows. 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 assumes that any class with a
$
in its name is a nested class.
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Represents a class that can be loaded throughClassPath.ClassInfo.load()
.(package private) static final class
Represents a single location (a directory or a jar file) in the class path and is responsible for scanning resources from this location.static class
Represents a class path resource that can be either a class file or any other resource file loadable from the class path. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String
private static final Splitter
Separator for the Class-Path manifest attribute value in jar files.private static final Logger
private final ImmutableSet
<ClassPath.ResourceInfo> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ClassPath
from
(ClassLoader classloader) Returns aClassPath
representing all classes and resources loadable fromclassloader
and its ancestor class loaders.Returns all classes loadable from the current class path.private static ImmutableList
<URL> getClassLoaderUrls
(ClassLoader classloader) (package private) static String
getClassName
(String filename) (package private) static ImmutableMap
<File, ClassLoader> getClassPathEntries
(ClassLoader classloader) (package private) static URL
getClassPathEntry
(File jarFile, String path) Returns the absolute uri of the Class-Path entry value as specified in JAR File Specification.(package private) static ImmutableSet
<File> getClassPathFromManifest
(File jarFile, Manifest manifest) Returns the class path URIs specified by theClass-Path
manifest attribute, according to JAR File Specification.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 ispackageName
or starts withpackageName
followed by a '.'.(package private) static ImmutableSet
<ClassPath.LocationInfo> locationsFrom
(ClassLoader classloader) Returns all locations thatclassloader
and parent loaders load classes and resources from.(package private) static ImmutableList
<URL> Returns the URLs in the class path specified by thejava.class.path
system property.(package private) static File
-
Field Details
-
logger
-
CLASS_PATH_ATTRIBUTE_SEPARATOR
Separator for the Class-Path manifest attribute value in jar files. -
CLASS_FILE_NAME_EXTENSION
- See Also:
-
resources
-
-
Constructor Details
-
ClassPath
-
-
Method Details
-
from
Returns aClassPath
representing all classes and resources loadable fromclassloader
and its ancestor class loaders.Warning:
ClassPath
can find classes and resources only from:URLClassLoader
instances'file:
URLs- the system class loader. To search the
system class loader even when it is not a
URLClassLoader
(as in Java 9),ClassPath
searches the files from thejava.class.path
system 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 ispackageName
or starts withpackageName
followed by a '.'. -
locationsFrom
Returns all locations thatclassloader
and parent loaders load classes and resources from. Callers can scan individual locations selectively or even in parallel. -
getClassPathFromManifest
Returns the class path URIs specified by theClass-Path
manifest attribute, according to JAR File Specification. Ifmanifest
is null, it means the jar file has no manifest, and an empty set will be returned. -
getClassPathEntries
-
getClassLoaderUrls
-
parseJavaClassPath
Returns the URLs in the class path specified by thejava.class.path
system property. -
getClassPathEntry
Returns the absolute uri of the Class-Path entry value as specified in JAR File Specification. Even though the specification only talks about relative urls, absolute urls are actually supported too (for example, in Maven surefire plugin).- Throws:
MalformedURLException
-
getClassName
-
toFile
-