0

I am trying to create a Http-server with akka api in scala, but I am getting the NoClassDefFoundError.

I checked Akka-Http 2.4.9 throws java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory exception and Akka-Http 2.4.9 throws java.lang.NoClassDefFoundError: akka/actor/ActorRefFactory exception

but I can't relate them to my case.

/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

Here is the my scala code,

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())
  }
}

Here is a my build.sbt, if that can be helpful.

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()
  )
}
Jayendra Parmar
  • 702
  • 12
  • 30
  • 1
    Why do you mark your dependencies as "provided"? If you do so it is supposed that those dependencies are provided at runtime by some "container" hosting your application, e.g. web server . I'm not sure you actually do that. So I suggest to remove all "provided" markers. That should fix the issue. – igorpcholkin Jan 21 '18 at 20:05
  • Thanks, @igorpcholkin that solved my issue, I am new to scala and I followed this https://hortonworks.com/tutorial/setting-up-a-spark-development-environment-with-scala/ where sbt file configured in that way. – Jayendra Parmar Jan 22 '18 at 03:16

1 Answers1

1

You can update your build.sbt to below as the version of akka you are referencing seems old

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
    )
  )
osleonard
  • 595
  • 5
  • 22