Class ClassPath

java.lang.Object
com.google.common.reflect.ClassPath

public final class ClassPath extends Object
Scans the source of a 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:

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
  • Field Details

    • logger

      private static final Logger logger
    • CLASS_PATH_ATTRIBUTE_SEPARATOR

      private static final Splitter CLASS_PATH_ATTRIBUTE_SEPARATOR
      Separator for the Class-Path manifest attribute value in jar files.
    • CLASS_FILE_NAME_EXTENSION

      private static final String CLASS_FILE_NAME_EXTENSION
      See Also:
    • resources

      private final ImmutableSet<ClassPath.ResourceInfo> resources
  • Constructor Details

  • Method Details

    • from

      public static ClassPath from(ClassLoader classloader) throws IOException
      Returns a ClassPath representing all classes and resources loadable from classloader and its ancestor class loaders.

      Warning: ClassPath can find classes and resources only from:

      Throws:
      IOException - if the attempt to read class path resources (jar files or directories) failed.
    • getResources

      public ImmutableSet<ClassPath.ResourceInfo> 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

      public ImmutableSet<ClassPath.ClassInfo> getAllClasses()
      Returns all classes loadable from the current class path.
      Since:
      16.0
    • getTopLevelClasses

      public ImmutableSet<ClassPath.ClassInfo> getTopLevelClasses()
      Returns all top level classes loadable from the current class path. Note that "top-level-ness" is determined heuristically by class name (see ClassPath.ClassInfo.isTopLevel()).
    • getTopLevelClasses

      public ImmutableSet<ClassPath.ClassInfo> getTopLevelClasses(String packageName)
      Returns all top level classes whose package name is packageName.
    • getTopLevelClassesRecursive

      public ImmutableSet<ClassPath.ClassInfo> getTopLevelClassesRecursive(String packageName)
      Returns all top level classes whose package name is packageName or starts with packageName followed by a '.'.
    • locationsFrom

      static ImmutableSet<ClassPath.LocationInfo> locationsFrom(ClassLoader classloader)
      Returns all locations that classloader and parent loaders load classes and resources from. Callers can scan individual locations selectively or even in parallel.
    • getClassPathFromManifest

      static ImmutableSet<File> getClassPathFromManifest(File jarFile, @CheckForNull Manifest manifest)
      Returns the class path URIs specified by the Class-Path manifest attribute, according to JAR File Specification. If manifest is null, it means the jar file has no manifest, and an empty set will be returned.
    • getClassPathEntries

      static ImmutableMap<File,ClassLoader> getClassPathEntries(ClassLoader classloader)
    • getClassLoaderUrls

      private static ImmutableList<URL> getClassLoaderUrls(ClassLoader classloader)
    • parseJavaClassPath

      static ImmutableList<URL> parseJavaClassPath()
      Returns the URLs in the class path specified by the java.class.path system property.
    • getClassPathEntry

      static URL getClassPathEntry(File jarFile, String path) throws MalformedURLException
      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

      static String getClassName(String filename)
    • toFile

      static File toFile(URL url)