Class ClasspathUtils


  • public class ClasspathUtils
    extends Object
    Classpath discovery utilities
    • Constructor Detail

      • ClasspathUtils

        public ClasspathUtils()
    • Method Detail

      • getSystemClasspath

        @Deprecated
        public static URL[] getSystemClasspath()
        Deprecated.
        since 7.0.1
        This method will return an array of URLs used by the system classloader.
        Returns:
        Array of URLs in classpath lookup order
      • getThreadContextClasspath

        public static URL[] getThreadContextClasspath()
        This method will return an array of URLs used by the thread context classloader and its parent classloaders.
        Returns:
        Array of URLs loaded in the classpath.
      • getThreadContentClassLoaderHierarchy

        public static List<ClassLoader> getThreadContentClassLoaderHierarchy()
        Get a list of classloaders in the thread context classloader hierarchy
        Returns:
        list of classloaders in order of delegation
      • getClassLoaderClasspath

        public static List<URL> getClassLoaderClasspath​(ClassLoader classloader)
        Get the classpath searched for by a particular classloader (only supports URLClassLoaders presently)
        Parameters:
        classloader - to examine
        Returns:
        List of paths searched on by the classloader, or null if the classloader does not support path discovery
      • getClassesFromJar

        public static List<String> getClassesFromJar​(URL jarUrl)
        Get a list of class files in the JAR specified by a URL.
        Parameters:
        jarUrl - the URL of a JAR on the classpath, such as one of the items in the list returned by getClassLoaderClasspath(ClassLoader)
        Returns:
        a List of Strings which are the class files in the JAR, an empty list if the JAR contains no class files or null if the JAR cannot be opened.
      • getClassesInClasspathJars

        public static ClasspathClasses getClassesInClasspathJars()
        Gets a list of JARs from the thread context classloader, and returns a ClasspathClasses object with the class file names and the JARs which they appear in. Useful for finding duplicate classes in the classpath.
        Returns:
        a ClasspathClasses object contains classes found in JARs on the classpath. If the thread context classloader is not a URLClassLoader, returns null.
        See Also:
        getThreadContextClasspath()
      • getFilesInClasspathJars

        public static ClasspathClasses getFilesInClasspathJars()
        Gets a list of JARs from the thread context classloader, and returns a ClasspathClasses object with the class file names and the JARs which they appear in. Useful for finding duplicate classes in the classpath.
        Returns:
        a ClasspathClasses object contains classes found in JARs on the classpath. If the thread context classloader is not a URLClassLoader, returns null.
        See Also:
        getThreadContextClasspath()
      • getJarFileFromClass

        public static Optional<File> getJarFileFromClass​(Class clazz)
        If the provided class has been loaded from a jar file, will find the absolute path to that jar file.
        Parameters:
        clazz - The jar file that contained the class file that represents this class will be found.