Scala - 如何使用Java Singleton对象

时间:2014-11-10 16:35:22

标签: java scala singleton sbt scala-2.11

我已成功使用现有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时,它正在我正在使用的另一个java依赖项目中。

1 个答案:

答案 0 :(得分:0)

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

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

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