简体   繁体   English

加载jar文件时找不到Java类

[英]Java class is not found when loading jar file

I am working on a API system for a program. 我正在为程序设计API系统。 This system goes to a 'plugin' folder and loads every jar there. 该系统转到“插件”文件夹,并在其中加载每个jar。 I am trying to load the main class of the jar file that is in the 'plugin' folder, but while doing so, I get a ClassNotFoundException. 我正在尝试加载“插件”文件夹中的jar文件的主类,但是这样做时,我得到了ClassNotFoundException。

Here is my code: 这是我的代码:

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);
}

If the question is vague or unclear, please let me know (I am new around here, so I try my best to word any question I ask). 如果问题不清楚或不清楚,请告诉我(我在这里是新来的,所以我会尽力说出我要问的任何问题)。

EDIT: Forgot to include the stack trace. 编辑:忘记包括堆栈跟踪。 Here it is! 这里是!

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)

Either the jar does not contain the requested class (check with a zip-tool or jar -tf DiamondCorePlugin.jar , or the jar-URL is not correct (it seems to point to a resource inside the jar, not the jar itself). You can create it a little easier like: jar不包含请求的类(使用zip工具或jar -tf DiamondCorePlugin.jar ,或者jar-URL不正确(它似乎指向jar中的资源,而不是jar本身)。您可以像下面这样轻松创建它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM