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>