A simple example of custom ClassLoader

Source: Internet
Author: User

Guidance:


Test1.java:

 

Package test;

 

Import java. io .*;
Import java. lang. reflect .*;

 

Class AClassLoader extends ClassLoader {
Public Class loadClass (String name, boolean resolve) throws ClassNotFoundException {
Class klass = null;
Try {
Klass = findLoadedClass (name); // check whether the class has been loaded.
If (klass! = Null ){
Return klass;
}

Byte [] bs = getClassBytes (name); // retrieves and reads bytes of this class from a specific information source.
If (bs! = Null & bs. length> 0 ){
Klass = defineClass (name, bs, 0, bs. length );
}
If (klass = null) {// if the reading of the byte fails, the system API of JDK tries to find the class.
Klass = findSystemClass (name );
}
If (resolve & klass! = Null ){
ResolveClass (klass );
}
} Catch (IOException e ){
Throw new ClassNotFoundException (e. toString ());
}
System. out. println ("klass =" + klass );
Return klass;
}
Private byte [] getClassBytes (String className) throws IOException {
String path = System. getProperty ("java. class. path") + File. separator;
Path + = className. replace (., File. separatorChar) + ". class ";
System. out. println (path );
FileInputStream FCM = null;
Try {
FCM = new FileInputStream (path );
} Catch (FileNotFoundException e ){
System. out. println (e );
Return null; // If the query fails, the query is abandoned. This exception is captured mainly to filter JDK system APIs.
}
Byte [] bs = new byte [FCM. available ()];
FS. read (bs );
Return bs;
}
}

 

Public class Test1 {
Public static void main (String [] args) throws Exception {
AClassLoader loader = new AClassLoader ();
Class c = loader. loadClass ("test. A", false );
System. out. println (c );
Object o = c. newInstance ();
System. out. println (o );
Method m = c. getMethod ("print", java. lang. String. class );
M. invoke (o, "bitan ");
}
}

 

 

 

 

 

A. java

 

Package test;
Public class {
Public void print (String str ){
System. out. println ("hi," + str + ", you finally found me! ");
}
Public String toString (){
Return "I am an object! ";
}
}

 

 

 

Result:

 

 

 

C: est> javac-d. in. src *. java

 

C: est> java-classpath. in test. Test1
. InestA. class
. InjavalangObject. class
Java. io. FileNotFoundException:. injavalangObject. class (the system cannot find the specified path.
Path .)
Klass = class java. lang. Object
Klass = class test.
Class test.
I am an object!
. InjavalangString. class
Java. io. FileNotFoundException:. injavalangString. class (the system cannot find the specified path.
Path .)
Klass = class java. lang. String
. InjavalangSystem. class
Java. io. FileNotFoundException:. injavalangSystem. class (the system cannot find the specified path.
Path .)
Klass = class java. lang. System
. InjavalangStringBuilder. class
Java. io. FileNotFoundException:. injavalangStringBuilder. class (the system cannot find
Path .)
Klass = class java. lang. StringBuilder
. InjavaioPrintStream. class
Java. io. FileNotFoundException:. injavaioPrintStream. class (the specified
Path .)
Klass = class java. io. PrintStream
Hi bitan, you finally found me!

 

C: est>

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.