Class ClasspathHelper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ClassLoader[]classLoaders(ClassLoader... classLoaders) Returns an array of class Loaders initialized from the specified array.static StringCleans the URL.static ClassLoaderGets the current thread context class loader.private static Collection<URL> distinctUrls(Collection<URL> urls) static URLforClass(Class<?> aClass, ClassLoader... classLoaders) Returns the URL that contains aClass.static Collection<URL> Returns a distinct collection of URLs based on URLs derived from class loaders.static Collection<URL> forClassLoader(ClassLoader... classLoaders) Returns a distinct collection of URLs based on URLs derived from class loaders.static Collection<URL> Returns a distinct collection of URLs based on thejava.class.pathsystem property.static Collection<URL> Returns a distinct collection of URLs based on URLs derived from class loaders expanded with Manifest information.static Collection<URL> forManifest(Iterable<URL> urls) Returns a distinct collection of URLs by expanding the specified URLs with Manifest information.static Collection<URL> forManifest(URL url) Returns a distinct collection of URLs from a single URL based on the Manifest information.static Collection<URL> forPackage(String name, ClassLoader... classLoaders) Returns a distinct collection of URLs based on a package name.static Collection<URL> forResource(String resourceName, ClassLoader... classLoaders) Returns a distinct collection of URLs based on a resource.static URLforWebInfClasses(javax.servlet.ServletContext servletContext) Returns the URL of theWEB-INF/classesfolder.static Collection<URL> forWebInfLib(javax.servlet.ServletContext servletContext) Returns a distinct collection of URLs based on theWEB-INF/libfolder.private static StringresourceName(String name) static ClassLoaderGets the class loader of this library.(package private) static URLtryToGetValidUrl(String workingDir, String path, String filename)
-
Constructor Details
-
ClasspathHelper
public ClasspathHelper()
-
-
Method Details
-
contextClassLoader
Gets the current thread context class loader.Thread.currentThread().getContextClassLoader().- Returns:
- the context class loader, may be null
-
staticClassLoader
Gets the class loader of this library.Reflections.class.getClassLoader().- Returns:
- the static library class loader, may be null
-
classLoaders
Returns an array of class Loaders initialized from the specified array.If the input is null or empty, it defaults to both
contextClassLoader()andstaticClassLoader()- Returns:
- the array of class loaders, not null
-
forPackage
Returns a distinct collection of URLs based on a package name.This searches for the package name as a resource, using
ClassLoader.getResources(String). For example,forPackage(org.reflections)effectively returns URLs from the classpath containing packages starting withorg.reflections.If the optional
ClassLoaders are not specified, then bothcontextClassLoader()andstaticClassLoader()are used forClassLoader.getResources(String).The returned URLs retainsthe order of the given
classLoaders.- Returns:
- the collection of URLs, not null
-
forResource
Returns a distinct collection of URLs based on a resource.This searches for the resource name, using
ClassLoader.getResources(String). For example,forResource(test.properties)effectively returns URLs from the classpath containing files of that name.If the optional
ClassLoaders are not specified, then bothcontextClassLoader()andstaticClassLoader()are used forClassLoader.getResources(String).The returned URLs retains the order of the given
classLoaders.- Returns:
- the collection of URLs, not null
-
forClass
Returns the URL that contains aClass.This searches for the class using
ClassLoader.getResource(String).If the optional
ClassLoaders are not specified, then bothcontextClassLoader()andstaticClassLoader()are used forClassLoader.getResources(String).- Returns:
- the URL containing the class, null if not found
-
forClassLoader
Returns a distinct collection of URLs based on URLs derived from class loaders.This finds the URLs using
URLClassLoader.getURLs()using bothcontextClassLoader()andstaticClassLoader().The returned URLs retains the order of the given
classLoaders.- Returns:
- the collection of URLs, not null
-
forClassLoader
Returns a distinct collection of URLs based on URLs derived from class loaders.This finds the URLs using
URLClassLoader.getURLs()using the specified class loader, searching up the parent hierarchy.If the optional
ClassLoaders are not specified, then bothcontextClassLoader()andstaticClassLoader()are used forClassLoader.getResources(String).The returned URLs retains the order of the given
classLoaders.- Returns:
- the collection of URLs, not null
-
forJavaClassPath
Returns a distinct collection of URLs based on thejava.class.pathsystem property.This finds the URLs using the
java.class.pathsystem property.The returned collection of URLs retains the classpath order.
- Returns:
- the collection of URLs, not null
-
forWebInfLib
Returns a distinct collection of URLs based on theWEB-INF/libfolder.This finds the URLs using the
ServletContext.The returned URLs retains the order of the given
classLoaders.- Returns:
- the collection of URLs, not null
-
forWebInfClasses
Returns the URL of theWEB-INF/classesfolder.This finds the URLs using the
ServletContext.- Returns:
- the collection of URLs, not null
-
forManifest
Returns a distinct collection of URLs based on URLs derived from class loaders expanded with Manifest information.The
MANIFEST.MFfile can contain aClass-Pathentry that defines additional jar files to be included on the classpath. This method finds the jar files using thecontextClassLoader()andstaticClassLoader(), before searching for any additional manifest classpaths.- Returns:
- the collection of URLs, not null
-
forManifest
Returns a distinct collection of URLs from a single URL based on the Manifest information.The
MANIFEST.MFfile can contain aClass-Pathentry that defines additional jar files to be included on the classpath. This method takes a single URL, tries to resolve it as a jar file, and if so, adds any additional manifest classpaths. The returned collection of URLs will always contain the input URL.- Returns:
- the collection of URLs, not null
-
forManifest
Returns a distinct collection of URLs by expanding the specified URLs with Manifest information.The
MANIFEST.MFfile can contain aClass-Pathentry that defines additional jar files to be included on the classpath. This method takes each URL in turn, tries to resolve it as a jar file, and if so, adds any additional manifest classpaths. The returned collection of URLs will always contain all the input URLs.The returned URLs retains the input order.
- Returns:
- the collection of URLs, not null
-
tryToGetValidUrl
-
cleanPath
-
resourceName
-
distinctUrls
-