【问题标题】:Class Not Found Exception When Creating New Instance Of Class Inside of Jar - Java在 Jar 内创建类的新实例时找不到类异常 - Java
【发布时间】:2015-02-06 02:55:10
【问题描述】:

我正在尝试创建一个类的新实例来调用 Java 中的方法。但是因为这个类在一个罐子里(加载得很好),这个类不能。这会导致抛出 ClassNotFoundException。有人可以告诉我如何解决这个问题吗?

代码:

private static void loadClassFromJar(String PluginJar) throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
    File PluginFile = new File("./debug/plugins/DiamondCorePlugin.jar");
    URL[] PluginURLs = { PluginFile.getAbsoluteFile().toURI().toURL() };
    URLClassLoader ClassLoader = URLClassLoader.newInstance(PluginURLs);
    Class<?> PluginClass = ClassLoader.loadClass("net.trenterprises.diamondcore.plugin.Main");
    Method EventMethod = PluginClass.getMethod("onEnable");
    EventMethod.invoke(PluginClass.newInstance());
}

堆栈跟踪:

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 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at net.trenterprises.diamondcore.cross.api.javaplugin.sub.Server.getClassCaller(Server.java:23)
at net.trenterprises.diamondcore.cross.api.javaplugin.sub.Server.getLogger(Server.java:15)
at net.trenterprises.diamondcore.plugin.Main.onEnable(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at net.trenterprises.diamondcore.cross.api.PluginLoader.loadClassFromJar(PluginLoader.java:55)
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)

【问题讨论】:

  • 你确定./debug/plugins/DiamondCorePlugin.jar在你运行你的应用程序时存在吗?如果没有,你能抛出异常吗 (if (!PluginFile.isFile()) throw new RuntimeException()) 看看会发生什么?
  • @vanza 是的,我确定它是一个文件,jar 加载正常(我运行了一个 if 语句,就像你告诉我的那样)。但是类没有加载。
  • 然后,检查 (i) 类文件是否存在于 jar 中并且 (ii) 它所依赖的所有类也存在。您可能还想使用getClass().getClassLoader()Thread.currentThread().getContextClassLoader() 作为第二个参数调用URLClassLoader.newInstance()
  • 异常不是来自您发布的代码。
  • @immibis 是对的。堆栈跟踪显示正在调用Main.onEnable()。所以错误在其他地方。

标签: java api class jar constructor


【解决方案1】:

发现问题了!事实证明,该类加载得很好,但是在另一个试图获取类名的类中,因为该类位于 JAR 文件中。它无法加载!感谢您对 immibiz 和 vanza 的所有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多