我正在为一个程序开发一个API系统。这个系统进入了一个插件'文件夹并加载每个罐子。我正在尝试加载'插件中的jar文件的主类。文件夹,但在这样做时,我得到一个ClassNotFoundException。
这是我的代码:
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 :(得分:1)
jar不包含所请求的类(使用zip工具或jar -tf DiamondCorePlugin.jar
检查,或者jar-URL不正确(它似乎指向jar内的资源,而不是jar本身)你可以更容易地创建它:
File file = new File("debug/plugins/DiamondCorePlugin.jar");
URL[] urls = { file.getAbsoluteFile().toURI().toURL() };