about classloaders. what do they do? in java code, we simply do this: in java code, we simply do...
TRANSCRIPT
About ClassLoadersAbout ClassLoaders
曾俊雄曾俊雄
What Do They Do?What Do They Do?
In java code, we simply do this:In java code, we simply do this: java.util.Hashtable table=new java.util.Hashtjava.util.Hashtable table=new java.util.Hasht
able();able();
At runtime, how do the VM know the defiAt runtime, how do the VM know the definition of java.util.Hashtable ??nition of java.util.Hashtable ??
ClassLoaders deal with that !!ClassLoaders deal with that !!
Basically, they search the CLASSPATHs, Basically, they search the CLASSPATHs, look into directories and jars, and load bylook into directories and jars, and load bytes from them……tes from them……
Where are They?Where are They?
There are at least three classloaders runThere are at least three classloaders running when VM is executing.ning when VM is executing. one for core java classesone for core java classes one for extended libs (jre/lib/ext)one for extended libs (jre/lib/ext) one for client classesone for client classes
of course, we can programmatically add as of course, we can programmatically add as many classloaders as we like!!many classloaders as we like!!
Why We Need Them?Why We Need Them?
We have a bunch of user-developed comWe have a bunch of user-developed componentsponents
in order to execute them, we need classloadin order to execute them, we need classloaders to load them into memory at first!!ers to load them into memory at first!!
However……However……
they are running simultaneouslythey are running simultaneously
they have their own classes, dependent jarsthey have their own classes, dependent jars
what if the classes have the same name?what if the classes have the same name?
ClassLoader
component1 component2
com.food.Milk ver 1.0
com.food.Milk ver 1.1
The solution:The solution: We have to separate their classloaders, i.e., We have to separate their classloaders, i.e.,
we have to load component1 from a classloawe have to load component1 from a classloader while load component2 from another clader while load component2 from another classloader.ssloader.
Let’s see some rules of thumb for classloadeLet’s see some rules of thumb for classloaders……rs……
Classloaders are organized hierarchically Classloaders are organized hierarchically into a forestinto a forest
but, by default, the system classloader will bbut, by default, the system classloader will be their common root (parent classloader)e their common root (parent classloader)
On searching for a class, the request will On searching for a class, the request will be delegated to the parent classloader at be delegated to the parent classloader at firstfirst
that is, if class com.food.Milk has been incluthat is, if class com.food.Milk has been included in a parent classloader and its child clasded in a parent classloader and its child classloader, the definition within the child classlosloader, the definition within the child classloader will never be usedader will never be used
Instances from classes defined in differenInstances from classes defined in difference classloaders are always different and ce classloaders are always different and not cast-able……not cast-able……
public interface IActionExecuterpublic interface IActionExecuter
public class ActionExecuter implements Ipublic class ActionExecuter implements IActionExecuterActionExecuter
ClassLoader A ClassLoader B
IActionExecuter
IActionExecuter
ActionExecuter
cast/convert ok!!cast/convert faill!!
Let’s go back to the question:Let’s go back to the question:
We have a bunch of user-developed We have a bunch of user-developed componentscomponents
We don’t want them to interfere with each We don’t want them to interfere with each other (but common interfaces should be other (but common interfaces should be cast-able)cast-able)
common classloader
kernel libs
interfaces
abstract classes
component classloader 1 component classloader 2
only component-specific implementations
and resources
only component-specific implementations
and resources
Sample CodeSample Code
public class ExtURLClassLoader extends URLClassLoader{
@Override public void addURL(URL arg0) { super.addURL(arg0); }
public ExtURLClassLoader(URL [] urls, ClassLoader parentClassLoader) { super(urls, parentClassLoader); } public ExtURLClassLoader(URL [] urls) { super(urls); }}
ExtURLClassLoader component1ClassLoader=new ExtURLClassLoader(new URL[0], Main.class.getClassLoader());
ExtURLClassLoader component2ClassLoader=new ExtURLClassLoader(new URL[0], Main.class.getClassLoader());
component1ClassLoader.addURL(new File("component1.jar").toURI().toURL());component2ClassLoader.addURL(new File("component2.jar").toURI().toURL());
IActionExecuter actionExecuter1=(IActionExecuter) component1ClassLoader.loadClass(props1.getProperty("ActionExecuter")).newInstance();IActionExecuter actionExecuter2=(IActionExecuter) component2ClassLoader.loadClass(props2.getProperty("ActionExecuter")).newInstance();