I am trying to build a simple web service with Akka-http. I followed this guide: http://doc.akka.io/docs/akka/2.4.9/scala/http/low-level-server-side-api.html and everything works fine, when I run sbt run.
When I execute java -jar PROJECT.jar it returns:
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
My 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"
)
And my Main object:
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
}
}
I already found java.lang.NoSuchMethodError: akka.actor.ActorCell.addFunctionRef and https://github.com/sbt/sbt-assembly/issues/204 but deleting the Akka-Actor in my scala lib folder hasn't helped. I also tried Adding akka-actor as dependency in the same version as akka-http.
sbt assembly
? this will generate a jar file with all the needed dependencies that can be run withjava -jar PROJECT.jar
– Josep Prat