| 10 | public class XlassLoader extends ClassLoader { |
| 11 | |
| 12 | public static void main(String[] args) throws Exception { |
| 13 | // 相关参数 |
| 14 | final String packageName = "lib"; |
| 15 | final String className = "Hello"; |
| 16 | final String methodName = "hello"; |
| 17 | // 创建类加载器 |
| 18 | ClassLoader classLoader = new XlassLoader(); |
| 19 | // 加载相应的类 |
| 20 | Class<?> clazz = classLoader.loadClass(packageName + "." + className); |
| 21 | // 看看里面有些什么方法 |
| 22 | for (Method m : clazz.getDeclaredMethods()) { |
| 23 | System.out.println(clazz.getSimpleName() + "." + m.getName()); |
| 24 | } |
| 25 | // 创建对象 |
| 26 | Object instance = clazz.getDeclaredConstructor().newInstance(); |
| 27 | // 调用实例方法 |
| 28 | Method method = clazz.getMethod(methodName); |
| 29 | method.invoke(instance); |
| 30 | } |
| 31 | |
| 32 | @Override |
| 33 | protected Class<?> findClass(String name) throws ClassNotFoundException { |