Scala - How to use Java Singleton Object

775 views Asked by At

I have been successfully been using existing Java projects for my Scala project but ran into a NoClassDefFoundError when using a Java singleton:

public class SpecificUser extends BasicUser {

    private static SpecificUser INSTANCE = new SpecificUser();

    protected SpecificUser() { }

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

Here is the exact error:

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]

These questions might have helped identify that accessing static methods may be inaccessible in Scala, but haven't helped identify a workable solution:

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

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

How do I go about using my singleton object in Scala?

UPDATE

I created a singleton in java dependency project I am using and it worked fine. The problem is when the singleton inherits from BasicUser, which is in another java dependency project i am using.

1

There are 1 answers

0
Wolfgang Liebich On

A NoClassDefFoundError can occur if the offending class has static fields whose initialization fails with an exception. The problem in looking this up is that the exception which causes class initialization to fail does not show up in any stacktrace or log. In this case it helps if the initialization of all static fields is moved to a static initializer. In this case you get at least a stacktrace showing you the offending field. I.e. instead of

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

you write

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