NoClassDefFoundError:Scala中的akka​​ / actor / ActorRefFactory

时间:2018-01-21 17:43:15

标签: scala noclassdeffounderror akka-http

我正在尝试使用scala中的akka​​ api创建一个Http-server,但我得到的是NoClassDefFoundError

我检查了Akka-Http 2.4.9 throws java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory exceptionAkka-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()
  )
}

1 个答案:

答案 0 :(得分:1)

您可以将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
    )
  )