我正在尝试用scala中的akka api创建一个Http-server,但是我得到了NoClassDefFoundError。
我查了Akka-Http 2.4.9 throws java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory exception和Akka-Http 2.4.9 throws java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory exception
但我不能把它们和我的案子联系起来。
/usr/lib/jvm/default-runtime/bin/java ...
Exception in thread "main" java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory
at main.scala.Main.main(main.scala)
Caused by: java.lang.ClassNotFoundException: akka.actor.ActorRefFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more这是我的scala代码,
package main.scala
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn
object Main{
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine()
bindingFuture
.flatMap(_.unbind())
.onComplete(_ => system.terminate())
}
}这是我的build.sbt,如果这对你有帮助的话。
name := "aiengine"
version := "0.1"
scalaVersion := "2.11.4"
libraryDependencies ++= {
val sparkVer = "2.1.0"
Seq(
"org.apache.spark" %% "spark-core" % sparkVer % "provided" withSources(),
"com.typesafe.akka" %% "akka-http" % "10.1.0-RC1" % "provided" withSources(),
"com.typesafe.akka" %% "akka-stream" % "2.5.9" % "provided" withSources()
)
}发布于 2018-01-22 04:58:56
您可以将您的build.sbt更新到下面,因为您引用的akka版本似乎很旧
lazy val akkaHttpVersion = "10.0.10"
lazy val akkaVersion = "2.5.4"
lazy val root = (project in file(".")).
settings(
inThisBuild(List(
organization := "com.company-name.application",
scalaVersion := "2.12.3"
)),
name := "aiengine",
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % Test,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test,
"com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % Test,
"org.scalatest" %% "scalatest" % "3.0.1" % Test
)
)https://stackoverflow.com/questions/48369811
复制相似问题