Dog. Java
 
 
 Public ClassDog {PublicDog () {system. Out. println ("Dog is loaded by:" +This. Getclass (). getclassloader ());}} 
 
 
View code
 
 
 
Sample. Java
 
 
Public ClassSample {Private IntV1 = 1;PublicSample () {system. Out. println ("Sample is loaded by:" +This. Getclass (). getclassloader ());NewDog ();}} 
 
 
View code
 
 
 
Myclassloader. Java
 
 
 Import  Java. Io. bytearrayoutputstream;  Import Java. Io. file;  Import  Java. Io. fileinputstream;  Import  Java. Io. filenotfoundexception;  Import  Java. Io. ioexception;  Import  Java. Io. inputstream;  Public   Class Myclassloader Extends  Classloader {  Private  String name; Private String Path = "f :\\" ;  Private   Static   Final String filetype = ". Class" ;  Public  Myclassloader (string name ){  //  The system class loader is the parent class loader of this class.          Super  ();  This . Name = Name ;} Public  Myclassloader (classloader parent, string name ){  //  Define your parent Loader          Super  (Parent );  This . Name = Name ;}  Protected Class <?> Findclass (string name) Throws  Classnotfoundexception {  Byte [] B = Loadclassdata (name ); Return Defineclass (name, B, 0 , B. Length );}  Private   Byte  [] Loadclassdata (string name) {inputstream is = Null  ;  Byte [] DATA = Null  ; Bytearrayoutputstream baos = Null  ;  This . Name =This . Name. Replace (".","\\" );  Try  {Is = New Fileinputstream ( New File (path + name + Filetype); baos = New  Bytearrayoutputstream ();  Int Ch = 0 ;  While (-1! = (CH =Is. Read () {baos. Write (CH);} data = Baos. tobytearray ();  Return  Data ;}  Catch  (Filenotfoundexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace ();}  Catch  (Ioexception e ){  //  Todo auto-generated Catch Block E. printstacktrace ();}  Finally  {  Try  {Is. Close (); baos. Close ();}  Catch  (Ioexception e ){  //  Todo auto-generated Catch Block  E. printstacktrace () ;}} system. Out. println ( "Failed to read the bytecode. " );  Return  Null  ;}  Public   Static   Void Main (string [] ARGs) Throws  Exception {myclassloader loader1 = New Myclassloader ("loader1" ); Loader1. setpath ( "F: \ kuaipan \ work \ j2se_workspace \ custom_classloader \ serverlib \\" ); Myclassloader loader2 = New Myclassloader (loader1, "loader2"); Loader2. setpath ( "F: \ kuaipan \ work \ j2se_workspace \ custom_classloader \ clientlib \\" );  //  The parent loader is the root loader. Myclassloader loader3 = New Myclassloader ( Null , "Loader3" ); Loader3. setpath ( "F: \ kuaipan \ work \ j2se_workspace \ custom_classloader \ otherlib \\" ); Test (loader2); test (loader3); Class objclass = Loader1.loadclass ("sample" ); Object OBJ =Objclass. newinstance ();}  Public   Static   Void Test (classloader loader) Throws  Exception {class classtype = Loader. loadclass ("sample" ); Object = Classtype. newinstance ();}  Public  String tostring (){  Return   This  . Name ;} Public  String getpath (){  Return  Path ;}  Public   Void  Setpath (string path ){  This . Path = Path ;}}  
 
View code