ClassNotFoundException用于依赖于运行时的sbt

时间:2018-10-19 09:19:32

标签: scala playframework sbt play-ws

我想使用播放框架中的StandaloneAhcWSClient。因此,我有以下代码:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.libs.ws.ahc.{AhcWSClientConfigFactory, StandaloneAhcWSClient}
import com.typesafe.config.ConfigFactory
...
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val httpClient = StandaloneAhcWSClient(AhcWSClientConfigFactory.forConfig(ConfigFactory.load(), classLoader = this.getClass.getClassLoader))

build.sbt中具有以下依赖性:

scalaVersion := "2.11.11",
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.6" % "test",
libraryDependencies += "com.typesafe.play" %% "play-ws-standalone-json" % "2.0.0-M6",
libraryDependencies += "com.typesafe.play" %% "play-ahc-ws-standalone" % "2.0.0-M6",
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.7.2",
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3",
libraryDependencies += "com.typesafe.akka" %% "akka-stream" % "2.5.12"

可以使用sbt 0.13.17进行编译。当我运行sbt package并执行jar文件中的代码时,出现此错误:

java.lang.RuntimeException: java.lang.NoClassDefFoundError: akka/stream/Materializer
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:498)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103)
    at scala.Option.map(Option.scala:146)
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103)
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96)
    at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524)
    at play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524)
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560)
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560)
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:537)
    at play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:537)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.NoClassDefFoundError: akka/stream/Materializer
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructors(Class.java:1651)
    at org.silkframework.runtime.plugin.PluginDescription$.getConstructor(PluginDescription.scala:148)
    at org.silkframework.runtime.plugin.PluginDescription$.getParameters(PluginDescription.scala:155)
    at org.silkframework.runtime.plugin.PluginDescription$.createFromClass(PluginDescription.scala:126)
    at org.silkframework.runtime.plugin.PluginDescription$.apply(PluginDescription.scala:99)
    at org.silkframework.runtime.plugin.PluginRegistry$.registerPlugin(PluginRegistry.scala:202)
    at org.silkframework.runtime.plugin.PluginRegistry$$anonfun$registerJars$1.apply(PluginRegistry.scala:180)
    at org.silkframework.runtime.plugin.PluginRegistry$$anonfun$registerJars$1.apply(PluginRegistry.scala:180)
    at scala.collection.immutable.List.foreach(List.scala:392)
    at org.silkframework.runtime.plugin.PluginRegistry$.registerJars(PluginRegistry.scala:180)
    at org.silkframework.runtime.plugin.PluginRegistry$.<init>(PluginRegistry.scala:44)
    at org.silkframework.runtime.plugin.PluginRegistry$.<clinit>(PluginRegistry.scala)
    at org.silkframework.runtime.users.WebUserManager$.instance(WebUserManager.scala:36)
    at org.silkframework.runtime.users.WebUserManager$.apply(WebUserManager.scala:50)
    at controllers.core.RequestUserContextAction$$anonfun$apply$3.apply(UserContextAction.scala:30)
    at controllers.core.RequestUserContextAction$$anonfun$apply$3.apply(UserContextAction.scala:29)
    at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:408)
    at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:407)
    at play.api.mvc.Action$.invokeBlock(Action.scala:533)
    at play.api.mvc.Action$.invokeBlock(Action.scala:530)
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:493)
    ... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: akka.stream.Materializer
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:814)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 45 common frames omitted

我已经用sbt shellshow runtime:fullClasspath中检查了我的类路径,这向我显示了这些条目:

[info] * Attributed(/Users/peter/.ivy2/cache/com.typesafe.akka/akka-stream_2.11/jars/akka-stream_2.11-2.5.12.jar)
[info] * Attributed(/Users/peter/.ivy2/cache/com.typesafe.akka/akka-actor_2.11/jars/akka-actor_2.11-2.5.12.jar)
[info] * Attributed(/Users/peter/.ivy2/cache/com.typesafe.akka/akka-protobuf_2.11/jars/akka-protobuf_2.11-2.5.12.jar)

我还尝试做一个sbt clean package,删除了我的~/.ivy2文件夹和一个~/.sbt文件夹,但该文件夹仍无法解决。

为什么会发生这种情况,我该如何解决?

0 个答案:

没有答案