Search code examples
scalaplayframeworkjson4s

NoClassDefFound json 4s


I'm stuck with NoClassDefFound error in scala since two days ago i'm using play framework and i want to display my object as json to the client side i used json4s (required this lib), i was using GSON in java, the code is getting NoClassDefFound when it tries to execute

implicit val formats = DefaultFormats

i added its dependencies which my team used in another 5 project but im stuck with this, here are the dependencies

libraryDependencies += "org.json4s" % "json4s-native_2.10" % "3.3.0"
libraryDependencies += "org.json4s" % "json4s-jackson_2.10" % "3.3.0"

i have search the whole google and i got no solution here is the stacktrace

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class org.json4s.DefaultFormats$]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:182)
    at play.core.server.AkkaHttpServer$$anonfun$$nestedInanonfun$executeHandler$1$1.applyOrElse(AkkaHttpServer.scala:251)
    at play.core.server.AkkaHttpServer$$anonfun$$nestedInanonfun$executeHandler$1$1.applyOrElse(AkkaHttpServer.scala:250)
    at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:412)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70)
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:68)
    at scala.concurrent.impl.Promise$KeptPromise$Kept.onComplete(Promise.scala:368)
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class org.json4s.DefaultFormats$
    at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:424)
    at play.api.mvc.Action.$anonfun$apply$2(Action.scala:96)
    at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$4(Accumulator.scala:174)
    at scala.util.Try$.apply(Try.scala:209)
    at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$3(Accumulator.scala:174)
    at scala.Function1.$anonfun$andThen$1(Function1.scala:52)
    at scala.Function1.$anonfun$andThen$1(Function1.scala:52)
    at scala.Function1.$anonfun$andThen$1(Function1.scala:52)
    at scala.Function1.$anonfun$andThen$1(Function1.scala:52)
    at play.api.libs.streams.StrictAccumulator.run(Accumulator.scala:207)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.json4s.DefaultFormats$
    at core.controllers.HelthyCheck.$anonfun$helthyCheck$1(HelthyCheck.scala:33)
    at play.api.mvc.ActionBuilder.$anonfun$apply$11(Action.scala:363)
    at scala.Function1.$anonfun$andThen$1(Function1.scala:52)
    at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:482)
    at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:480)
    at play.api.mvc.ActionBuilder$$anon$9.invokeBlock(Action.scala:331)
    at play.api.mvc.ActionBuilder$$anon$9.invokeBlock(Action.scala:326)
    at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:419)
    at play.api.mvc.Action.$anonfun$apply$2(Action.scala:96)
    at play.api.libs.streams.StrictAccumulator.$anonfun$mapFuture$4(Accumulator.scala:174)

Solution

  • Just Change scala version which in sbt from 2.12.3 to 2.11.7 it will work fine..