2012-08-17 75 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