Java Utililty Methods ClassPath

List of utility methods to do ClassPath

Description

The list of methods to do ClassPath are organized into topic(s).

Method

SetfindClasspathsByLoader(ClassLoader loader)
find Classpaths By Loader
Set<URL> urls = new HashSet<URL>();
if (loader instanceof URLClassLoader) {
    URLClassLoader urlLoader = (URLClassLoader) loader;
    urls.addAll(Arrays.asList(urlLoader.getURLs()));
} else {
    Enumeration<URL> urlEnum;
    try {
        urlEnum = loader.getResources("");
...
EnumerationfindClassPathsToEn()
find Class Paths To En
List<URL> urls = findClassPaths();
final Iterator<URL> iterator = urls.iterator();
return new Enumeration<URL>() {
    @Override
    public boolean hasMoreElements() {
        return iterator.hasNext();
    @Override
...
StringfindDirectoryFromClasspath(Class cls, String file)
Determine a directory's real path from the classpath.
URL rootMarker = cls.getResource(file);
String markerPath = rootMarker.getPath();
File markerFile = new File(markerPath);
return markerFile.getParentFile().getAbsolutePath() + "/";
URLfindResourceOnClasspath(String resourceName)
Looks for a resource on the classpath with the given name
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return Optional.ofNullable(classLoader.getResource(resourceName))
        .orElseThrow(() -> new IllegalArgumentException("Resource not found with name: " + resourceName));
StringgetAvailableClassPathInfo(final ClassLoader classLoader)
get Available Class Path Info
if (!(classLoader instanceof URLClassLoader)) {
    return "-";
final URLClassLoader cl = (URLClassLoader) classLoader;
return Arrays.toString(cl.getURLs());
URL[]getBootstrapClassPath()
get Bootstrap Class Path
return getBootstrapURLs();
ClassLoadergetDriverClassLoader(String driverClasspath)
Provides a ClassLoader suitable for loading SQL driver class.
if (driverClasspath == null || driverClasspath.length() == 0) {
    return Thread.currentThread().getContextClassLoader();
} else {
    String pathSeparator = System.getProperty("path.separator");
    String[] elements = driverClasspath.split(pathSeparator);
    URL[] urls = new URL[elements.length];
    for (int i = 0; i < elements.length; i++) {
        urls[i] = new URL("file", "", elements[i].trim());
...
FilegetFileFromClasspath(final String fileName)
Load the resource either using current class loader or using parent class loader
File file = null;
URL url = Thread.currentThread().getClass().getResource(fileName);
if (url != null) {
    file = new File(url.getFile());
} else {
    url = Thread.currentThread().getContextClassLoader().getResource(fileName);
    if (url != null) {
        file = new File(url.getFile());
...
FilegetFileFromClasspath(String fileName)
get File From Classpath
URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (url == null)
    return null;
File file = null;
try {
    file = new File(url.toURI());
} catch (URISyntaxException e) {
    file = new File(url.getPath());
...
StringgetFileFromClasspath(String name)
get File From Classpath
URL url = ClassLoader.getSystemResource(name);
if (url == null) {
    throw new IllegalArgumentException("Could not find " + name);
return url.getPath();