1.1.responsibilities of the class loader
each class (Class) has a class loader. The duties of the ClassLoader ClassLoader include the following:
(1) the positioning of the class. Depending on the class name , you can find the corresponding class file, or you may not need a class file.
(2) the definition of the class. An instance of the class class that produces the definition of the expression class .
a the code for the Sample class is as follows:
Package com.test.javatechnology.classloader.test; // The class file for sample classes will be located in the test directory. Publicclass Sample {//nothing. }
Custom- made the code for Myclassloader is as follows:
/*** @Title: myclassloader.java* @Package com.test.javatechnology.classloader* @Description:*@author http://www.cnblogs.com/coe2coe/* @date April 4, 2017 pm 9:52:47*@versionV1.0*/ PackageCom.test.javatechnology.classloader;Importjava.io.IOException;ImportJava.io.InputStream;/*** @ClassName: myclassloader* @Description:*@author http://www.cnblogs.com/coe2coe/* @date April 4, 2017 pm 9:52:47**/ Public classMyclassloaderextendsClassLoader {@OverrideprotectedClass<?> Findclass (String name)throwsClassNotFoundException {//get the full class file name. String FullName= "test/" + Name + ". Class"; System.out.println (fullname); InputStream is=NULL; Class Clazz=NULL;byte[] bytes =NULL; Try{//reads the contents of the class file. is= Myclassloader.class. getResourceAsStream (fullname);intSize =is.available (); bytes=New byte[Size];is.read (bytes);//defines a class. Clazz=Super. DefineClass (Bytes, 0, size);}Catch(Exception e) {e.printstacktrace ();}finally{if(is!=NULL){Try{is.close ();=NULL;} Catch(IOException e) {e.printstacktrace ();}}if(bytes!=NULL) {bytes=NULL;}}returnClazz;} }
The test code is as follows:
Public Static voidMain (string[] args)throwsclassnotfoundexception {Class clazz=NULL; ClassLoader Loader=NULL; Myclassloader ClassLoader=Newmyclassloader (); loader=ClassLoader; while(loader!=NULL) {System.out.println (Loader.getclass (). GetName ()); loader=loader.getparent ();} SYSTEM.OUT.PRINTLN (loader); System.out.println ("-------------"); //the full fully qualified name of the class////clazz = Classloader.loadclass ("Com.test.javatechnology.classloader.test.Sample");//only simple names are written. Under normal circumstances, it cannot be loaded. However, the Myclassloader has been specially processed so that it can be loaded successfully. Clazz= Classloader.loadclass ("Sample"); loader=Clazz.getclassloader (); while(loader!=NULL) {System.out.println (Loader.getclass (). GetName ()); loader=loader.getparent ();} SYSTEM.OUT.PRINTLN (loader); }
The results of the operation are as follows:
Com.test.javatechnology.classloader.MyClassLoader
Sun.misc.launcher$appclassloader
Sun.misc.launcher$extclassloader
Null
-------------
Test/sample.class
Com.test.javatechnology.classloader.MyClassLoader
Sun.misc.launcher$appclassloader
Sun.misc.launcher$extclassloader
Null
The results of the operation are analyzed as follows:
(1) because it is not under the same package ,java comes with class loader appclassloader extclassloader bootstrap ClassLoader are not likely to find sample class file. According to the process of loadclass () method, this will be done myclassloader findclass () method.
(2) Myclassloader 's findclass () method, successfully found and read the class of the Sample file, and then call the defineclass () method to complete the definition of the sample class to successfully load another class that is not under the same package, sample class.
Responsibilities for loaders such as Java Important Technology (29) ClassLoader