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

booleanaddDirectoryToClassLoader(ClassLoader loader, File directory)
add Directory To Class Loader
try {
    ClassLoader rootLoader = getRootLoader(loader);
    if (rootLoader == null) {
        return false;
    } else {
        URL url = directory.toURI().toURL();
        Method method = rootLoader.getClass().getMethod("addURL", URL.class);
        method.invoke(rootLoader, url);
...
voidaddFileToSystemClassLoader(String s)
add File To System Class Loader
File f = new File(s);
addFileToSystemClassLoader(f);
ClassLoadercreateClassloader(File libDir, ClassLoader cl)
Create a class loader that can load classes from the specified file directory.
URLClassLoader urlClassloader = null;
if (libDir.exists()) {
    if (libDir.isDirectory()) {
        String[] jars = libDir.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar") || name.endsWith(".zip");
        });
...
URLClassLoadercreateClassLoader(File root)
create Class Loader
return new URLClassLoader(new URL[] { root.toURI().toURL() },
        Thread.currentThread().getContextClassLoader());
ClassLoadercreateClassLoader(final String[] cpEntries)
Builds a ClassLoader for a given classpath.
final URL[] cpUrls = new URL[cpEntries.length];
for (int i = 0; i < cpEntries.length; i++) {
    String cpEntry = cpEntries[i];
    File entry = new File(cpEntry);
    try {
        URL url = entry.toURI().toURL();
        cpUrls[i] = url;
    } catch (MalformedURLException ex) {
...
URLClassLoadercreateClassLoader(String fileName)
create Class Loader
String urlPath = makeUrlPath(fileName);
URL url = null;
try {
    url = new URL(urlPath);
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
URLClassLoader loader = URLClassLoader.newInstance(new URL[] { url });
...
URLClassLoadercreateFallbackClassLoader(Collection files)
create Fallback Class Loader
List<URL> urls = new ArrayList<URL>(files.size());
for (File file : files) {
    urls.add(file.toURI().toURL());
return new URLClassLoader(urls.toArray(new URL[urls.size()]), null);
voiddumpClassLoader(ClassLoader cl, PrintWriter out)
dump Class Loader
int level = 0;
while (cl != null) {
    String msg = "Servlet ClassLoader[" + level + "]: " + cl.getClass().getName() + ':' + cl.hashCode();
    out.println(msg);
    URL[] urls = getClassLoaderURLs(cl);
    msg = "  URLs:";
    out.println(msg);
    for (int u = 0; u < urls.length; u++) {
...
ClassgetClass(Object classLoaderMakedObject, File file, String targetClass)
get Class
URL[] urls = { file.toURI().toURL() };
URLClassLoader classLoader = (URLClassLoader) classLoaderMakedObject.getClass().getClassLoader();
ClassLoader loader = URLClassLoader.newInstance(urls, classLoader);
Class<?> clazz = loader.loadClass(targetClass);
return clazz;
InputStreamgetClassInputStream(String className, ClassLoader cl)
get Class Input Stream
return cl.getResourceAsStream(classToResource(className));