2010-05-03 167 views
5

我與這樣一個scala代碼的回聲服務。斯卡拉遠程演員異常

進口scala.actors.Actor 進口scala.actors.Actor._ 進口scala.actors.remote.RemoteActor._

 
class Echo extends Actor { 
    def act() { 
     alive(9010) 
     register('myName, self) 

    loop { 
     react { 
      case msg => println(msg) 
     } 
    } 
    } 
} 
object EchoServer { 

    def main(args: Array[String]): unit = { 
     val echo = new Echo 
     echo.start 
     println("Echo server started") 
    } 
} 
EchoServer.main(null) 

但有一些例外。

 
java.lang.NoClassDefFoundError: Main$$anon$1$Echo$$anonfun$act$1 
at Main$$anon$1$Echo.act((virtual file):16) 
at scala.actors.Reaction.run(Reaction.scala:76) 
at scala.actors.Actor$$anonfun$start$1.apply(Actor.scala:785) 
at scala.actors.Actor$$anonfun$start$1.apply(Actor.scala:783) 
at scala.actors.FJTaskScheduler2$$anon$1.run(FJTaskScheduler2.scala:160) 
at scala.actors.FJTask$Wrap.run(Unknown Source) 
at scala.actors.FJTaskRunner.scanWhileIdling(Unknown Source) 
at scala.actors.FJTaskRunner.run(Unknown Source) 
Caused by: java.lang.ClassNotFoundException: Main$$anon$1$Echo$$anonfun$act$1 
at java.net.URLClassLoader$1.run(URLClassLoader.java:200) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(URLClassLoader.java:188) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:307) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:252) 
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) 
... 8 more 

我不知道怎麼會引起它。 順便說一句.my scala的版本是2.7.5

回答

3

ClassNotFoundException表示某些東西可能沒有編譯過,應該已經編譯好了。你是如何編譯它的?手動使用scalac

請嘗試以下操作rm *.classscalac *.scalascala EchoServer

以下工作:


EchoServer.scala

import scala.actors.Actor 
import scala.actors.Actor._ 
import scala.actors.remote.RemoteActor._ 

class Echo extends Actor { 
    def act() { 
     alive(9010) 
     register('myName, self) 

    loop { 
     react { 
      case msg => println(msg) 
     } 
    } 
    } 
} 

object EchoServer { 

    def main(args: Array[String]): unit = { 
     val echo = new Echo 
     echo.start 
     println("Echo server started") 
    } 
} 

Client.scala

import scala.actors.Actor._ 
import scala.actors.remote.Node 
import scala.actors.remote.RemoteActor._ 

object Client extends Application { 
    override def main(args: Array[String]) { 
     if (args.length < 1) { 
      println("Usage: scala Client [msg]") 
      return 
     } 

     actor { 
      val remoteActor = select(Node("localhost", 9010), 'myName) 

      remoteActor !? args(0) match { 
       case msg => println("Server's response is [" + msg + "]") 
      } 
     } 
    } 
} 

命令行:

rm *.class && scalac *.scala && scala EchoServer 

而在其他終端:

scala Client hello