日期:2014-05-20 浏览次数:20752 次
//BasicPlugin.java package byv; abstract class BasicPlugin { abstract public int test(int a); } //PluginA.java package byv; import byv.BasicPlugin; public class PluginA extends BasicPlugin { @Override public int test(int a) { return a + a; } } //主程序 private static void loadTest() throws Exception { URL url = new File("PluginA.jar").toURI().toURL(); URLClassLoader ClassLoader = URLClassLoader.newInstance(new URL[] {url}); Class<?> clazz = Class.forName("byv.PluginA", true, ClassLoader); BasicPlugin obj = (BasicPlugin) clazz.newInstance(); obj.test(2); }
Exception in thread "main" java.lang.IllegalAccessError: class byv.PluginA cannot access its superclass byv.BasicPlugin at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:634) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) at java.net.URLClassLoader.access$000(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:212) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:615) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at byv.Main.loadTest1(Main.java:18) at byv.Main.main(Main.java:11)