Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to run simple akka sample using sbt

Tags:

scala

akka

sbt

I'm just following akka sample but could not run the program.

  1. I've installed akka, sbt(0.13), scala(2.10.3) using homebrew(OSX Mountail Lion)
  2. make empty directory named akka_test
  3. create build.sbt and Hello.scala file
  4. run sbt in akka_test directory and compile command worked well
  5. sbt's run command complains No main class detected

What should I do to run the program?

here's my code

build.sbt

name := "My Project"

version := "1.0"

scalaVersion := "2.10.2"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies +=
  "com.typesafe.akka" %% "akka-actor" % "2.2.1"

Hello.scala

import akka.actor.Actor
import akka.actor.Props

class HelloWorld extends Actor {

  override def preStart(): Unit = {
    // create the greeter actor
    val greeter = context.actorOf(Props[Greeter], "greeter")
    // tell it to perform the greeting
    greeter ! Greeter.Greet
  }

  def receive = {
    // when the greeter is done, stop this actor and with it the application
    case Greeter.Done ⇒ context.stop(self)
  }
}

object Greeter {
  case object Greet
  case object Done
}

class Greeter extends Actor {
  def receive = {
    case Greeter.Greet ⇒
      println("Hello World!")
      sender ! Greeter.Done
  }
}
like image 610
Hongseok Yoon Avatar asked Oct 11 '13 07:10

Hongseok Yoon


1 Answers

sbt has run-main command which accepts main class from command line, so full command is

sbt "run-main akka.Main HelloWorld"
like image 186
Ponimas Avatar answered Sep 19 '22 18:09

Ponimas