我正在尝试使用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.addFunctionRef和https://github.com/sbt/sbt-assembly/issues/204但是删除我的scala lib文件夹中的Akka-Actor没有帮助。我还尝试在与akka-http相同的版本中添加akka-actor作为依赖项。
答案 0 :(得分:1)
Sbt正在使用java本机应用程序打包来打包scala项目。所以你不必使用sbt组装或创建一个胖罐。 你可以尝试这些方法:
在项目根目录中:
sbt universal:packageBin
运行:
./target/universal/stage/bin/[project name]
答案 1 :(得分:0)
添加到project/plugins.sbt
:
resolvers += Resolver.url("bintray-sbt-plugins", url("https://dl.bintray.com/eed3si9n/sbt-plugins/"))(Resolver.ivyStylePatterns)
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")
sbt compile
sbt assembly
最后输入.jar
文件,输入scala target/scala-2.12/YOUR_FILE.assembly-0.1.jar
,其中scala-2.12
是您的scala版本。
答案 2 :(得分:0)
我在带有 play
的 2.8.8
版本 play-sbt-plugin 2.8.2
上遇到了这个异常。当我将 play-sbt-plugin
更新为 2.8.8
时,异常消失了。