tags:

views:

64

answers:

1

This is the code I used from a book...

import scala.actors.Actor._

val countActor = actor {
  loop {
    react {
      case "how many?" => {
        println("I've got " + mailboxSize.toString + " messages in my mailbox.")
      }
    }
  }
}

countActor ! 1
countActor ! 2
countActor ! 3
countActor ! "how many?"
countActor ! "how many?"
countActor ! 4
countActor ! "how many?"

The error

java.lang.NoClassDefFoundError: Main$$anon$1$$anonfun$1$$anonfun$apply$2

+3  A: 

I'm guessing you are executing with just scala rather than compiling. The script does work if you compile it (and wrap it in an Application-trait singleton object):

import scala.actors.Actor._
object ActorTest extends Application {
  val countActor = actor {
    loop {
        react {
            case "how many?" => { println("I've got " + mailboxSize.toString + " messages in my mailbox.") }
      }
    }
  }

  countActor ! 1
  countActor ! 2
  countActor ! 3
  countActor ! "how many?"
  countActor ! "how many?"
  countActor ! 4
  countActor ! "how many?"
}

// vim: set ts=4 sw=4 et:

When I compile that, I get the following files:

  • ActorTest$$anonfun$1$$anonfun$apply$2$$anonfun$apply$1.class
  • ActorTest$$anonfun$1$$anonfun$apply$2.class
  • ActorTest$$anonfun$1.class
  • ActorTest$.class
  • ActorTest.class

If I invoke it with scala -cp . ActorTest I get this:

ricoeur:~ tom$ scala -cp . ActorTest
I've got 6 messages in my mailbox.
I've got 5 messages in my mailbox.
I've got 4 messages in my mailbox.
^C

It sits and waits after the "I've got 4 messages in my mailbox" output until I Ctrl+C it.

Hope that helps.

Tom Morris
Ahh! Ya know..it does. =) Thanks.
Uruhara747
Do not mix actors with `Application`.
Daniel