public class RootLoader extends URLClassLoader
Note: this is very against the normal behavior of classloaders. Normal is to first check parent and then look in the resources you gave this classloader.
It's possible to add urls to the classpath at runtime through addURL(URL)
Why using RootLoader? If you have to load classes with multiple classloaders and a classloader does know a class which depends on a class only a child of this loader does know, then you won't be able to load the class. To load the class the child is not allowed to redirect it's search for the class to the parent first. That way the child can load the class. If the child does not have all classes to do this, this fails of course.
For example:
parentLoader (has classpath: a.jar;c.jar) | | childLoader (has classpath: a.jar;b.jar;c.jar)class C (from c.jar) extends B (from b.jar) childLoader.find("C") --> parentLoader does know C.class, try to load it --> to load C.class it has to load B.class --> parentLoader is unable to find B.class in a.jar or c.jar --> NoClassDefFoundException! if childLoader had tried to load the class by itself, there would be no problem. Changing childLoader to be a RootLoader instance will solve that problem.
Constructor and Description |
---|
RootLoader(LoaderConfiguration lc)
constructs a new RootLoader with a
LoaderConfiguration
object which holds the classpath |
RootLoader(URL[] urls,
ClassLoader parent)
constructs a new RootLoader with a parent loader and an
array of URLs as classpath
|
Modifier and Type | Method and Description |
---|---|
void |
addURL(URL url)
adds an url to the classpath of this classloader
|
protected Class |
findClass(String name) |
URL |
getResource(String name)
returns the URL of a resource, or null if it is not found
|
protected Class |
loadClass(String name,
boolean resolve)
loads a class using the name of the class
|
close, definePackage, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstance
defineClass, defineClass
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public RootLoader(URL[] urls, ClassLoader parent)
public RootLoader(LoaderConfiguration lc)
LoaderConfiguration
object which holds the classpathprotected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
loadClass
in class ClassLoader
ClassNotFoundException
public URL getResource(String name)
getResource
in class ClassLoader
public void addURL(URL url)
addURL
in class URLClassLoader
protected Class findClass(String name) throws ClassNotFoundException
findClass
in class URLClassLoader
ClassNotFoundException