62,634
社区成员




public class ClassLoaderTest {
public static void main(String[] args) throws Exception{
ClassLoader myLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try{
String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
InputStream is = getClass().getResourceAsStream(fileName);
if (is == null){
return super.loadClass(name);
}
byte[] b = new byte[is.available()];
is.read(b);
return defineClass(name, b, 0, b.length);
}catch (IOException e){
throw new ClassNotFoundException(name);
}
}
};
Object obj = myLoader.loadClass("editor4_1.test.jvm.ClassLoaderTest").newInstance();
ClassLoaderTest classLoaderTest = new ClassLoaderTest();
System.out.println("classLoaderTest's classLoader is " + classLoaderTest.getClass().getClassLoader());
System.out.println("obj's classLoader is " + obj.getClass().getClassLoader());
System.out.println(obj instanceof ClassLoaderTest);
}
}