5

I am trying to communicate between remote actors in scala using the Akka actors. My program runs fine from scala IDE but I am unable to run it from command prompt. In my project folder, I have the application.conf file, scala file and akka-remote_2.10-2.2.2.jar file. I am compiling my program as follows

scalac -cp akka-remote_2.10-2.2.2.jar FileName.scala

The program compiles and then I run it as follows

scala FileName

I get the following error

java.lang.ClassNotFoundException: akka.remote.RemoteActorRefProvider
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(Dynam
icAccess.scala:68)
    at akka.actor.ReflectiveDynamicAccess$$anonfun$getClassFor$1.apply(Dynam
icAccess.scala:67)
    at scala.util.Try$.apply(Try.scala:161)
    at akka.actor.ReflectiveDynamicAccess.getClassFor(DynamicAccess.scala:67
)
    at akka.actor.ReflectiveDynamicAccess.createInstanceFor(DynamicAccess.sc
ala:85)
    at akka.actor.ActorSystemImpl.<init>(ActorSystem.scala:546)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:111)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:93)
    at Project1Bonus2$.main(Project1Bonus2.scala:11)
    at Project1Bonus2.main(Project1Bonus2.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClass
Loader.scala:71)
    at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoade
r.scala:31)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaC
lassLoader.scala:139)
    at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scal
a:71)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLo
ader.scala:139)
    at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
    at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
    at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)

    at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala
:74)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)

    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

My application.conf file is as follows.

akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 9001
}
}
}

Any help would be appreciated.

Thanks

4

1 回答 1

0

这里的问题是 Scala 发行版默认在其引导类路径中包含 akka-actor 2.XX。因此,scalacscala在您的代码中使用的是不同的。

解决方法是使用 java 而不是 scala 来启动应用程序。您需要做的就是在类路径中包含正确的 Scala jar(scala-library 和 scala-compiler)。

java -cp akka-remote_2.10-2.2.2.jar FileName.scala

会有很多 jar 依赖,例如 akka-actor 等。

因此,使用 SBT 或将所有 jar 放在一个文件夹中,然后将该文件夹添加到类路径中。

java -cp ./jars/*:. FileName.scala
于 2020-05-21T09:22:03.163 回答