2015-02-06 71 views
1

我正在爲一個程序開發API系統。這個系統進入一個'插件'文件夾並在那裏加載每個jar文件。我正在嘗試加載'plugin'文件夾中的jar文件的主類,但這樣做時,我得到一個ClassNotFoundException異常。加載jar文件時未找到Java類

這裏是我的代碼:

private static void loadClassFromJar(String PluginJar) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException { 
    logger.debug("jar:file:" + "./debug/plugins/DiamondCorePlugin.jar!/"); 
    URL[] urls = { new URL("jar:file:" + FileList.PluginFolder.getAbsolutePath() + PluginJar +"!/") }; 
    URLClassLoader ClassLoader = URLClassLoader.newInstance(urls); 
    Class<?> Class = ClassLoader.loadClass("net.trenterprises.diamondcore.plugin.Main"); 
    Object Object = Class.newInstance(); 
    Method EventMethod = Object.getClass().getMethod("onEnable"); 
    EventMethod.invoke(Object); 
} 

如果問題是含糊不清或不明確的,請讓我知道(我是新來的身邊,所以我盡我所能去的話,我問任何問題)。編輯: 忘記包含堆棧跟蹤。這裏是!

java.lang.ClassNotFoundException: net.trenterprises.diamondcore.plugin.Main 
at java.net.URLClassLoader$1.run(URLClassLoader.java:372) 
at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:360) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:798) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
at net.trenterprises.diamondcore.cross.api.PluginLoader.loadClassFromJar(PluginLoader.java:53) 
at net.trenterprises.diamondcore.cross.api.PluginLoader.loadAllPlugins(PluginLoader.java:25) 
at net.trenterprises.diamondcore.DiamondCoreServer.<init>(DiamondCoreServer.java:47) 
at net.trenterprises.diamondcore.run.main(run.java:15) 
+0

'trenterprises'似乎是一個陌生的名字,也許你的意思'enterprises' – 2015-02-06 01:17:44

回答

1

無論是罐子不包含請求的類(用拉鍊工具或jar -tf DiamondCorePlugin.jar,或者Jar-URL檢查是不正確的(似乎指向一個資源的罐子,不是罈子自身內部。)您可以創建更容易有點像:

File file = new File("debug/plugins/DiamondCorePlugin.jar"); 
URL[] urls = { file.getAbsoluteFile().toURI().toURL() }; 
+0

好吧,這個作品謝謝! – Whirvis 2015-02-06 02:02:45