2016-04-23 83 views
0

我正在爲插件做一個模塊。主應用程序加載插件,然後爲我的插件加載模塊(主應用程序 - 加載插件 - >我的插件 - 加載模塊 - >我的模塊)。從動態加載的罐子中使用主jar方法

我有一個jar(我們將其稱爲dynamicJar),我動態加載到我的插件。我的問題是,當我想用​​的方法和類從我dynamicJar插件我得到一個NoClassDefFound錯誤:

Caused by: java.lang.NoClassDefFoundError: me/venom/crates/objects/crates/Crate 
at me.venom.csgo.CSGOCrate.runCSGO(CSGOCrate.java:135) ~[CSGOCrates.jar:?] 
at me.venom.crates.CSGOHelper.runCSGO(CSGOHelper.java:58) ~[?:?] 
at me.venom.crates.PListener.onChestInteract(PListener.java:194) ~[?:?] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_75] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_75] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_75] 
at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_75] 
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.9.jar:git-Spigot-1480adb-8b61cc5] 
... 17 more 
Caused by: java.lang.ClassNotFoundException: me.venom.crates.objects.crates.Crate 
at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[?:1.7.0_75] 
at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[?:1.7.0_75] 
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_75] 
at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[?:1.7.0_75] 
at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[?:1.7.0_75] 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) ~[?:1.7.0_75] 
at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[?:1.7.0_75] 
at me.venom.csgo.CSGOCrate.runCSGO(CSGOCrate.java:135) ~[CSGOCrates.jar:?] 
at me.venom.crates.CSGOHelper.runCSGO(CSGOHelper.java:58) ~[?:?] 
at me.venom.crates.PListener.onChestInteract(PListener.java:194) ~[?:?] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_75] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_75] 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_75] 
at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_75] 
at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.9.jar:git-Spigot-1480adb-8b61cc5] 
... 17 more 

的問題是,我可以運行從主應用程序方法dynamicJar裏面,但我不能使用dynamicJar中插件的方法。

TL; DR:使用來自插件jar的類在從動態加載的jar中使用它們時會拋出ClassNotFoundException和NoClassDefFoundError。

編輯:

這裏是我使用的jar文件加載到類路徑中的代碼。

Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class}); 
    method.setAccessible(true); 
    method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()}); 
+0

這聽起來很像ClassLoader問題,您需要提供代碼,否則將很難提供幫助 –

+0

@NicolasFilotto使用代碼 –

+0

編輯主帖使用系統ClassLoader定義插件和模塊? –

回答

0

我通過將類加載到插件的類加載器而不是它自己的類加載器或系統類加載器來解決此問題。現在工作完美無瑕。

+0

很高興知道你可以自己找到答案 –