NoClassDefFound json 4s

时间:2017-11-06 14:13:52

标签: scala playframework json4s

我在scala中遇到NoClassDefFound错误,因为两天前我正在使用Play框架,我想将我的对象显示为json到客户端我使用了json4s(需要这个lib),我在java中使用GSON ,代码在尝试执行

时获得NoClassDefFound

implicit val formats = DefaultFormats

我添加了我的团队在另外5个项目中使用的依赖项,但我坚持这个, 这是依赖项

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

我搜索整个谷歌,我没有解决方案 这是堆栈跟踪

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)

1 个答案:

答案 0 :(得分:1)

只需将sbt中的scala版本从 2.12.3 更改为 2.11.7 即可正常工作..