Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "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();"— Presentation transcript:

1 About ClassLoaders

2 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(); java.util.Hashtable table=new java.util.Hashtable(); At runtime, how do the VM know the definition of java.util.Hashtable ?? At runtime, how do the VM know the definition of java.util.Hashtable ??

3 ClassLoaders deal with that !! ClassLoaders deal with that !! Basically, they search the CLASSPATHs, look into directories and jars, and load bytes from them…… Basically, they search the CLASSPATHs, look into directories and jars, and load bytes from them……

4 Where are They? There are at least three classloaders running when VM is executing. There are at least three classloaders running when VM is executing. one for core java classes one for core java classes one for extended libs (jre/lib/ext) one for extended libs (jre/lib/ext) one for client classes one for client classes of course, we can programmatically add as many classloaders as we like!! of course, we can programmatically add as many classloaders as we like!!

5 Why We Need Them? We have a bunch of user-developed components We have a bunch of user-developed components in order to execute them, we need classloaders to load them into memory at first!! in order to execute them, we need classloaders to load them into memory at first!!

6 However…… However…… they are running simultaneously they are running simultaneously they have their own classes, dependent jars they have their own classes, dependent jars what if the classes have the same name? what if the classes have the same name?

7 ClassLoader component1 component2 com.food.Milk ver 1.0 com.food.Milk ver 1.1

8 The solution: The solution: We have to separate their classloaders, i.e., we have to load component1 from a classloader while load component2 from another classloader. We have to separate their classloaders, i.e., we have to load component1 from a classloader while load component2 from another classloader. Lets see some rules of thumb for classloaders…… Lets see some rules of thumb for classloaders……

9 Classloaders are organized hierarchically into a forest Classloaders are organized hierarchically into a forest but, by default, the system classloader will be their common root (parent classloader) but, by default, the system classloader will be their common root (parent classloader)

10 On searching for a class, the request will be delegated to the parent classloader at first On searching for a class, the request will be delegated to the parent classloader at first that is, if class com.food.Milk has been included in a parent classloader and its child classloader, the definition within the child classloader will never be used that is, if class com.food.Milk has been included in a parent classloader and its child classloader, the definition within the child classloader will never be used

11 Instances from classes defined in difference classloaders are always different and not cast-able…… Instances from classes defined in difference classloaders are always different and not cast-able……

12 public interface IActionExecuter public interface IActionExecuter public class ActionExecuter implements IActionExecuter public class ActionExecuter implements IActionExecuter

13 ClassLoader AClassLoader B IActionExecuter ActionExecuter cast/convert ok!! cast/convert faill!!

14 Lets go back to the question: Lets go back to the question: We have a bunch of user-developed components We have a bunch of user-developed components We dont want them to interfere with each other (but common interfaces should be cast-able) We dont want them to interfere with each other (but common interfaces should be cast-able)

15 common classloader kernel libs interfaces abstract classes component classloader 1component classloader 2 only component- specific implementations and resources only component- specific implementations and resources

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

17 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")).newIns tance(); IActionExecuter actionExecuter2= (IActionExecuter) component2ClassLoader.loadClass(props2.getProperty("ActionExecuter")).newIns tance();


Download ppt "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();"

Similar presentations


Ads by Google