Custom Class Loader

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.