4
votes

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.

3
Have you tried using assembly plugin and then run sbt assembly? this will generate a jar file with all the needed dependencies that can be run with java -jar PROJECT.jarJosep Prat

3 Answers

1
votes

Sbt is using java native app packging for packing scala projects. So you don't have to use sbt assembly or create a fat jar. You can try something along these lines:

In project root:

sbt universal:packageBin

run:

 ./target/universal/stage/bin/[project name]
0
votes

Add to 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")
  • Check if your program compile by typing sbt compile
  • Then sbt assembly

At the end run your .jar file by typing scala target/scala-2.12/YOUR_FILE.assembly-0.1.jar where scala-2.12 is your scala version.

0
votes

I got this exception on play version 2.8.8 with play-sbt-plugin 2.8.2. When I updated play-sbt-plugin to 2.8.8 exception has gone.