about classloaders. what do they do? in java code, we simply do this: in java code, we simply do...

17
About ClassLoaders About ClassLoaders 曾曾曾 曾曾曾

Upload: antonio-dowd

Post on 27-Mar-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: About ClassLoaders. 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.Hashtable();

About ClassLoadersAbout ClassLoaders

曾俊雄曾俊雄

Page 2: About ClassLoaders. 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.Hashtable();

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 ??

Page 3: About ClassLoaders. 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.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……

Page 4: About ClassLoaders. 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.Hashtable();

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!!

Page 5: About ClassLoaders. 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.Hashtable();

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!!

Page 6: About ClassLoaders. 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.Hashtable();

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?

Page 7: About ClassLoaders. 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.Hashtable();

ClassLoader

component1 component2

com.food.Milk ver 1.0

com.food.Milk ver 1.1

Page 8: About ClassLoaders. 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.Hashtable();

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……

Page 9: About ClassLoaders. 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.Hashtable();

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)

Page 10: About ClassLoaders. 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.Hashtable();

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

Page 11: About ClassLoaders. 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.Hashtable();

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……

Page 12: About ClassLoaders. 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.Hashtable();

public interface IActionExecuterpublic interface IActionExecuter

public class ActionExecuter implements Ipublic class ActionExecuter implements IActionExecuterActionExecuter

Page 13: About ClassLoaders. 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.Hashtable();

ClassLoader A ClassLoader B

IActionExecuter

IActionExecuter

ActionExecuter

cast/convert ok!!cast/convert faill!!

Page 14: About ClassLoaders. 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.Hashtable();

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)

Page 15: About ClassLoaders. 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.Hashtable();

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

Page 16: About ClassLoaders. 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.Hashtable();

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); }}

Page 17: About ClassLoaders. 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.Hashtable();

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();