运行 Jar 时出现 NoClassDefFoundError

NoClassDefFoundError when running Jar

我有这个问题。当我 运行 我的代码在 Intellij 中时它工作正常,但如果我做一个工件并构建 jar,它就不起作用。我认为它是由外部库引起的。这是我的输出:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mindfusion/scheduling/Calendar
        at GUI.<init>(GUI.java:75)
        at Logfiles.main(Logfiles.java:13)
Caused by: java.lang.ClassNotFoundException: com.mindfusion.scheduling.Calendar
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 2 more

我知道是哪个 Class 但我不知道如何解决问题。我真的只是一个初学者。你能帮我解释一下吗?谢谢

编辑:

在我使用提取的库构建工件后出现此错误:错误:发生 JNI 错误,请检查您的安装并重试 线程“main”中的异常java.lang.SecurityException:清单主要属性的签名文件摘要无效

此错误仅表示 class 文件不在 jar 中。

一种可能的解决方案是您可以下载 jd-gui 用于查看 jars。您可以使用它来检查 class 是否存在。

另一个解决方案是您可以使用这个简单的命令在 jar 中 grep 搜索 class。

grep -l "<class-name>" <jar-name>.jar

如果 class 不在 jar 文件中。您可以使用 jar 命令添加 class。

jar -cvf <jar-absolute-location> <class-path>
eg : jar -cvf GUI.jar com.mindfusion.scheduling.Calendar

了解此问题的最简单方法是阅读该问题的 Javadoc class。来自 Javadoc:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

这意味着当特定的 class 在编译时存在但在运行时不知何故不可用时 NoClassDefFoundError 可以被抛出 。这可能是由于缺少 JAR 文件、权限问题、或不正确的 class运行时路径

通常,当开发人员忽略为使用的库定义 class 路径时,我会看到这些问题。他们忘记了您的 IDE 有自己的文件定义 class 路径(即 Eclipse 有 .classpath 文件)所以 运行 来自 IDE 的应用程序工作正常(class 在编译期间存在),但在编译应用程序后并且 class 路径未在托管应用程序的机器中定义,NoClassDefFoundError 被抛出(class 在运行时“丢失”)。

如果class路径正确,我的建议是先弄清楚。比none次数多,就是这个问题。如果 class路径正确,请确保所有权限设置正确。