Scala - How to use Java Singleton Object
I have successfully used existing Java projects for my Scala project, but ran into a NoClassDefFoundError when using the 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 may have helped determine that access to static methods might not be available in Scala, but did not help determine a workable solution:
How can I use my singleton object in Scala?
UPDATE
I created a singleton in a java dependency project that I am using and it worked fine. The problem is that the singleton inherits from BasicUser, which is in another java dependency project that I am using.
source to share
A NoClassDefFoundError can be raised if the offending class has static fields that are initialized with an exception. The problem is that the exception that is causing the class initialization to fail is not showing up in any stack 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 stack that shows you the resentment field. That is, instead of
class A {
static B b = new B(....);
}
you write
class A {
static B b;
static {
b=new B(...);
}
}
source to share