-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassLoaderTest.java
More file actions
41 lines (33 loc) · 1016 Bytes
/
Copy pathClassLoaderTest.java
File metadata and controls
41 lines (33 loc) · 1016 Bytes
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
38
39
40
41
package jvm;
import java.io.IOException;
import java.io.InputStream;
public class ClassLoaderTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ClassLoader myClassLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name)
throws ClassNotFoundException {
// TODO Auto-generated method stub
String filename = name.substring(name.lastIndexOf(".") + 1)
+ ".class";
InputStream is = getClass().getResourceAsStream(filename);
if (is == null) {
return super.loadClass(name);
}
byte[] b = null;
try {
b = new byte[is.available()];
is.read(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return defineClass(name, b, 0, b.length);
}
};
Object obj = myClassLoader.loadClass("jvm.ClassLoaderTest").newInstance();
System.out.println(obj.getClass());
System.out.println(obj instanceof jvm.ClassLoaderTest);
}
}