Update:
I need put libraryDependencies for using Actor in Scala.
libraryDependencies <++= scalaVersion(v =>
Seq("org.scala-lang" % "scala-actors" % v)
)
when I ran "sbt run", I could successfully pass the compilation but then failed on run the code.
Here is the error: java.lang.RuntimeException: No main class detected.
The weird thing is when I do not use SBT, I can run it without any issue:
>scalac actor.scala
>scala pingpong
Any idea why this happens?
I am using SBT 0.13.0 Scala version is 2.10.2
Thanks
Here is my code:
import scala.actors.Actor
import scala.actors.Actor._
case object Ping;
case object Pong;
case object Stop;
class Ping(count:Int, pong:Actor) extends Actor
{
def act()
{
var counter = count -1 ;
pong ! Ping;
loop
{
react
{
case Pong =>
if( (counter < count) && (counter > 0) ) {Console.println(counter+"->Ping: pong"); pong ! Ping; counter = counter -1;}
else {pong ! Stop;exit()}
}
}
}
}
class Pong extends Actor
{
def act()
{
loop
{
react
{
case Ping => {Console.println("Pong: Ping"); sender ! Pong;}
case Stop => {Console.println("Ping Pong Communication is done!");exit()}
}
}
}
}
object pingpong
{
def main(args: Array[String])
{
println(util.Properties.versionString)
val pong = new Pong
val ping = new Ping(5, pong)
ping.start
pong.start
}
}
Try this in your build file:
mainClass in (Compile,run) := Some("pingpong")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With