public class TestMyClassLoader {
static class MyClassLoader extends ClassLoader{
private String classPath;
public MyClassLoader(String classPath) {
this.classPath = classPath;
protected Class> findClass(String name) throws ClassNotFoundException {
byte[] data = loadByte(name);
return defineClass(name,data,0,data.length);
throw new ClassNotFoundException();
private byte[] loadByte(String name) throws IOException {
name = name.replaceAll("\\.","/");
FileInputStream fis = new FileInputStream(classPath + "/" + name + ".class");
int len = fis.available();
byte[] data = new byte[len];
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
MyClassLoader classLoader = new MyClassLoader("D:/test");
Class> clazz = classLoader.loadClass("com.zhouyu.Parent");
Object instance = clazz.newInstance();
Method method = clazz.getDeclaredMethod("test");
System.out.println(clazz.getClassLoader().getClass().getName());