gpt4 book ai didi

java - Scala - 如何使用 Java Singleton 对象

转载 作者:行者123 更新时间:2023-12-02 00:13:32 24 4
gpt4 key购买 nike

我已经成功地将现有 Java 项目用于我的 Scala 项目,但在使用 Java 单例时遇到了 NoClassDefFoundError:

public class SpecificUser extends BasicUser {

private static SpecificUser INSTANCE = new SpecificUser();

protected SpecificUser() { }

public static SpecificUser getInstance() {
return INSTANCE;
}
...
}

这是确切的错误:

play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class SpecificUser]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.5.jar:2.3.5]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.5.jar:2.3.5]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205) [play_2.11-2.3.5.jar:2.3.5]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202) [play_2.11-2.3.5.jar:2.3.5]
at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) [scala-library-2.11.1.jar:na]
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class SpecificUser
at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:523) ~[play_2.11-2.3.5.jar:2.3.5]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130) ~[play_2.11-2.3.5.jar:2.3.5]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130) ~[play_2.11-2.3.5.jar:2.3.5]
at play.utils.Threads$.withContextClassLoader(Threads.scala:21) ~[play_2.11-2.3.5.jar:2.3.5]
at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:129) ~[play_2.11-2.3.5.jar:2.3.5]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class SpecificUser
at controllers.UsersController$$anonfun$usersDatatable$1.apply(UsersController.scala:36) ~[classes/:na]
at controllers.UsersController$$anonfun$usersDatatable$1.apply(UsersController.scala:34) ~[classes/:na]
at controllers.security.Authentication$$anonfun$AuthenticatedAction$1.apply(AuthenticatedController.scala:21) ~[classes/:na]
at controllers.security.Authentication$$anonfun$AuthenticatedAction$1.apply(AuthenticatedController.scala:19) ~[classes/:na]
at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:433) ~[play_2.11-2.3.5.jar:2.3.5]

这些问题可能有助于确定在 Scala 中可能无法访问静态方法,但无助于确定可行的解决方案:

https://stackoverflow.com/a/4448069/1359765

https://stackoverflow.com/a/21303729/1359765

如何在 Scala 中使用我的单例对象?

更新

我在我正在使用的java依赖项项目中创建了一个单例,它工作得很好。问题是当单例继承自 BasicUser 时,BasicUser 位于我正在使用的另一个 java 依赖项项目中。

最佳答案

如果有问题的类具有初始化失败并出现异常的静态字段,则可能会发生 NoClassDefFoundError。查找此问题的问题是导致类初始化失败的异常不会显示在任何堆栈跟踪或日志中。在这种情况下,如果将所有静态字段的初始化移至静态初始值设定项,则会有所帮助。在这种情况下,您至少会得到一个堆栈跟踪,显示有问题的字段。IE。而不是

class A {
static B b = new B(....);
}

你写

class A {
static B b;
static {
b=new B(...);
}
}

关于java - Scala - 如何使用 Java Singleton 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848502/

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