我使用这样的scala代码进行echo服务。
导入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") } } 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
答案 0 :(得分:3)
ClassNotFoundException
表示可能未编译的内容应该已编译。你是怎么编译的?手动使用scalac
?
尝试以下rm *.class
scalac *.scala
scala 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
答案 1 :(得分:0)
您需要在远程actor上设置类加载器。
在act()
方法之前,添加以下行:
RemoteActor.classLoader = getClass.getClassLoader
Why is setting the classloader necessary with Scala RemoteActors?