gpt4 book ai didi

scala - Akka-Http 2.4.9 抛出 java.lang.NoClassDefFoundError : akka/actor/ActorRefFactory exception

转载 作者:行者123 更新时间:2023-12-01 04:53:16 26 4
gpt4 key购买 nike

我正在尝试使用 Akka-http 构建一个简单的 Web 服务。我遵循了本指南:http://doc.akka.io/docs/akka/2.4.9/scala/http/low-level-server-side-api.html当我运行 sbt run 时,一切正常。

当我执行 java -jar PROJECT.jar 它返回:

Exception in thread "main" java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory
at WebServer.main(WebServer.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

我的 build.sbt:
name := "APPNAME"

version := "0.0.1"

scalaVersion := "2.11.8"

artifactName := { (sv: ScalaVersion, module: ModuleID, artifact: Artifact) =>
artifact.name + "-" + module.revision + "." + artifact.extension
}

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http-core" % "2.4.9"
)

而我的主要对象:
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpMethods._
import akka.http.scaladsl.model._
import akka.stream.ActorMaterializer
import scala.io.StdIn

object WebServer {
def main(args: Array[String]) {

implicit val system = ActorSystem("APPNAME")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher

val requestHandler: HttpRequest => HttpResponse = {

case HttpRequest(GET, Uri.Path("/"), _, _, _) => {

HttpResponse(entity = HttpEntity(
ContentTypes.`text/html(UTF-8)`,
"<html><body>Hello world!</body></html>")
)

}

case HttpRequest(GET, Uri.Path("/crash"), _, _, _) => {
sys.error("BOOM!")
}

case r: HttpRequest => {
r.discardEntityBytes() // important to drain incoming HTTP Entity stream
HttpResponse(404, entity = "Unknown resource!")
}

}

val bindingFuture = Http().bindAndHandleSync(requestHandler, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done

}
}

我已经找到 java.lang.NoSuchMethodError: akka.actor.ActorCell.addFunctionRefhttps://github.com/sbt/sbt-assembly/issues/204但是删除我的 scala lib 文件夹中的 Akka-Actor 并没有帮助。我还尝试在与 akka-http 相同的版本中添加 akka-actor 作为依赖项。

最佳答案

Sbt 正在使用 Java 原生应用程序打包来打包 Scala 项目。所以你不必使用 sbt 程序集或创建一个 fat jar 。
您可以尝试以下方法:

在项目根目录中:

sbt universal:packageBin

跑:
 ./target/universal/stage/bin/[project name]

关于scala - Akka-Http 2.4.9 抛出 java.lang.NoClassDefFoundError : akka/actor/ActorRefFactory exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39309986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com