3

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.

Community
  • 1
  • 1
j will
  • 3,747
  • 11
  • 41
  • 64
  • 1
    What is the exact error and also how do you compile and run the project? Is this a mixed Java/Scala project, or is the Java code in a dependency? – drexin Nov 10 '14 at 16:44
  • I compile and run the project with activator. It is not a mixed java/scala project, a java project is being used as a dependency for the Scala Play project – j will Nov 10 '14 at 16:51
  • Try to use the same configuration from java class. Problem is, that SpecificUser class has exception in static block (on in superclass). Check all uncaught exception in debug mode. – Andrzej Jozwik Nov 10 '14 at 21:52
  • @AndrzejJozwik - What do you mean try to use the same configuration from java class? I'm using the java class with it's original configuration. I've tried checking for uncaught exceptions in debug mode, but the problem is that when I try to step in and use the getInstance() method the debugger goes to the scala class files like Action.scala then play Threads – j will Nov 11 '14 at 21:16
  • @jwill The class is loaded to virtual machine if initialization of all static blocks finished with success. If at least one failed - the class is not loaded. – Andrzej Jozwik Dec 18 '14 at 21:42
  • Perhaps you should switch to Spring and use its dependency injection. Just a thought – jlars62 Apr 01 '15 at 18:48

1 Answers1

0

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(...);
    }
}
Wolfgang Liebich
  • 400
  • 2
  • 12