2012-08-17 53 views
0

I am loading a plug-in dynamically. Both the plug-in and the software have been created by us.动态加载罐上的AbstractMethodError

I have an Interface lets call it Foo. There is also FooImpl that just implements that method But FooImpl is in the jar loaded dynamically public interface Foo { void write(..someArgument..) throws Exception; }

I have also a PluginLoader class here is the method

public 
Object loadPlugin(final String jarPath, final Class 
pluginInterface) { 
    try 
    { 
     final URI uri = new File(jarPath).toURI(); 
     final URL url = uri.toURL();

final URLClassLoader ucl = new URLClassLoader(new URL[] { url }); try { final Class<?> pluginClass = Class.forName("FooImpl", true, ucl); // Verify if plugin implements plugin interface. if (pluginClass.getInterfaces()[0].getName().equals(pluginInterface.getName())) { // Instantiate plugin. return pluginClass.newInstance(); } }//[...] </code></pre>

This part is actually working well i think so because after doing some sysout on the pluginClass i notice:
the .getMethods() =
[public void FooImpl.write(..someArgumentType..) throws Exception, public abstract void some.package.Foo.write(..someArgumentType..) throws Exception]

the .getGenericInterfaces() = [interface some.package.Foo]

But when i try to call the method write here is what i get
java.lang.AbstractMethodError: FooImpl.write(..SomeArgumentType..;)V
I dont know why there is a ";" and a "V"

So basically i think that it try to call the interface method instead of the implemented one. So i'm wondering What is going on!

As usual, Thank you for your time and help

+0

你是通过实例调用写入方法,即'((Foo)instance).write(??)'还是通过反射? – MadProgrammer 2012-08-17 14:59:13

+0

final PluginLoader loader = new PluginLoader(); final Object loadedLayer = loader.loadPlugin(pathToPlugin,Foo.class); this.plugin =(Foo)loadedLayer;我真的很抱歉,我没有使用stackoverFlow – drgn 2012-08-17 15:04:39

+0

如果(pluginClass instanceof pluginInterface)) - 不应该这是你的接口检查? – JamesB 2012-08-17 15:53:48

回答

0

AbstractMethodError表明您的代码正在试图在运行时使用不同版本的类,而不是它最初构建的类。您需要确保在执行环境中,类路径中没有流行的接口实现版本。

+0

Humm,我加载插件的唯一时间是通过OP中描述的pluginLoader类。我也必须给我加载实现的jar的路径。但我会尝试验证classLoader加载的内容。 – drgn 2012-08-17 15:40:33

+0

先生,你是我的英雄。我的班加载器确实有一个流氓实例。对于任何类似问题的人,只需在jvm选项中使用-verbose:class并调查该类的狂野流氓实例。感谢您的朋友 – drgn 2012-08-17 15:59:26

+0

不客气。 – JamesB 2012-08-17 18:21:21