很难的原因是安全性。类加载器是不可变的。你不应在运行时随意向其添加类。实际上,我很惊讶能与系统类加载器一起使用。这是制作自己的子类加载器的方法:
URLClassLoader child = new URLClassLoader( new URL[] {myJar.toURI().toURL()}, this.getClass().getClassLoader());Class classToLoad = Class.forName("com.MyClass", true, child);Method method = classToLoad.getDeclaredMethod("myMethod");Object instance = classToLoad.newInstance();Object result = method.invoke(instance);很痛苦,但确实如此。



