-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyClassLoaderTest.java
More file actions
37 lines (30 loc) · 1.12 KB
/
MyClassLoaderTest.java
File metadata and controls
37 lines (30 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package chapter10;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
/**
* Created by zhangzhao on 2018/8/27.
*/
public class MyClassLoaderTest {
public static void main(String[] args) throws ClassNotFoundException,IllegalAccessError,InstantiationError,NoSuchMethodException,
InvocationTargetException{
//声明一个myclassLoader
MyClassLoader classLoader = new MyClassLoader();
//使用MyClassLoader加载HelloWorld
Class<?> aClass = classLoader.loadClass("com.chapter10.HelloWorld");
System.out.println(aClass.getClassLoader());
//
Object helloWorld = null;
try {
helloWorld = aClass.newInstance();
System.out.println(helloWorld);
Method welcomeMethod = aClass.getMethod("welcome");
String result = (String)welcomeMethod.invoke(helloWorld);
System.out.println("Result: "+result);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}