Java Utililty Methods Class Loader

List of utility methods to do Class Loader

Description

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

Method

URLClassLoadergetClassLoader()
get Class Loader
return (URLClassLoader) URLClassLoader.getSystemClassLoader();
ClassLoadergetClassLoader(File[] directories)
get Class Loader
ClassLoader cl = null;
if (directories == null || directories.length == 0) {
    System.out.println("Directory is null or 0 length");
    return cl;
ArrayList urlList = new ArrayList();
for (int i = 0; i < directories.length; i++) {
    File directory = directories[i];
...
ClassLoadergetClassLoader(String path)
get Class Loader
try {
    File file = new File(path);
    URL url = file.toURL();
    return new URLClassLoader(new URL[] { url }, Thread.currentThread().getContextClassLoader());
} catch (Exception e) {
    return Thread.currentThread().getContextClassLoader();
ClassLoadergetClassLoaderForClass(final Class clazz)
Gets the class loader for a given class.
return clazz.getClassLoader();
ClassLoadergetClassLoaderForDirectory(final File baseFolder)
get Class Loader For Directory
final Lock readLock = LOCK.readLock();
readLock.lock();
if (CLASSLOADER_MAP.containsKey(baseFolder)) {
    readLock.unlock();
    return CLASSLOADER_MAP.get(baseFolder);
} else {
    final Lock writeLock = LOCK.writeLock();
    writeLock.lock();
...
FilegetClassloaderRootDir(Class forClass)
Provides a binaries root directory.
URL rootUrl = forClass.getClassLoader().getResource(".");
return new File(rootUrl.getFile());
CollectiongetClassLoaders(ClassLoader baseClassLoader)
get Class Loaders
Collection<URLClassLoader> loaders = new ArrayList<URLClassLoader>(8);
ClassLoader loader = baseClassLoader;
while (loader != null) {
    if ("sun.misc.Launcher$ExtClassLoader".equals(loader.getClass().getName())) {
        break;
    if (loader instanceof URLClassLoader) {
        loaders.add((URLClassLoader) loader);
...
URLClassLoader[]getClassLoaderStack(ClassLoader cl)
Get all of the URLClassLoaders from cl on up the hierarchy
ArrayList stack = new ArrayList();
while (cl != null) {
    if (cl instanceof URLClassLoader) {
        stack.add(cl);
    cl = cl.getParent();
URLClassLoader[] ucls = new URLClassLoader[stack.size()];
...
StringgetClassLocation(Class clazz)
Returns the location of the given class.
String className = "/" + clazz.getName().replace('.', '/') + ".class";
URL u = clazz.getResource(className);
String s = u.toString();
if (s.startsWith("jar:file:/")) {
    int pos = s.indexOf(".jar!/");
    if (pos != -1) {
        if (File.separator.equals("\\")) {
            s = s.substring("jar:file:/".length(), pos + ".jar".length());
...
StringgetClassLocation(String className)
Returns a string representing the location of the class definition.
Class myClass = Class.forName(className);
URL locationUrl = myClass.getProtectionDomain().getCodeSource().getLocation();
String location = locationUrl.toString();
if (location.startsWith("jar")) {
    locationUrl = ((JarURLConnection) locationUrl.openConnection()).getJarFileURL();
    location = locationUrl.toString();
if (location.startsWith("file")) {
...