gpt4 book ai didi

java - 具有私有(private)构造函数的非静态类上的 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-29 08:35:13 26 4
gpt4 key购买 nike

我有一个具有私有(private)构造函数的 Java 类:

public class MyClass {
private static final MyClass myClass = new MyClass();

private MyClass() {}

public static MyClass getInstance() {
return myClass;
}
}

这个类在应用程序中是这样使用的:

MyClass myClass = MyClass.getInstance();

整个应用程序也导出为 JAR 并在另一个应用程序中使用。

当我尝试在另一个应用程序(从 JAR 调用)中执行相同操作时,出现以下错误:

java.lang.NoClassDefFoundError: Could not initialize class com.example.MyClass

我不确定这是否是具有私有(private)构造函数 的类所必需的行为,还是它有其他问题?

谢谢!

最佳答案

您的错误与静态或您的构造函数无关。来自javadocs :

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.

NoClassDefFoundError 表示当您尝试运行程序时类定义不可用。这是某种路径错误 - 此类不在导出的 jar 中,或者在运行时未包含在类路径中。

关于java - 具有私有(private)构造函数的非静态类上的 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44658044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com