如何在Java应用程序中使用Akka Actors?

我想在Java中使用Akka actors.

我下载了akka-1.0.zip并在Eclipse中的“Build Path”中添加了akka-actor-1.0.jar.

然后我写了这个Actor类:

package com.example;

import akka.actor.UntypedActor;

public class MyActor extends UntypedActor {

    public void onReceive(Object message) throws IllegalArgumentException {
        if (message instanceof String) {
            System.out.println("Received: " + message);
        } else throw new IllegalArgumentException("Unknown message: " + message);
    }
}

但我在Eclipse中遇到错误:

The type scala.Option cannot be resolved.
The type scala.Some cannot be resolved.
The type scala.PartialFunction cannot be resolved.
The type scala.ScalaObject cannot be resoled.

我是否需要在“构建路径”中添加更多文件或者我做错了什么?我没有找到有用的文档.

更新:我将scala-library.jar添加到我的Build Path中,上面的错误消失了.但是在编译和运行应用程序时出现错误:

Exception in thread "main" java.lang.NoClassDefFoundError: net/lag/configgy/ConfigMap
    at akka.actor.Actors.actorOf(Actors.java:70)
    at com.example.ActorTest.main(ActorTest.java:9)
Caused by: java.lang.ClassNotFoundException: net.lag.configgy.ConfigMap
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

这是我使用我的演员的主要类:

package com.example;

import akka.actor.ActorRef;
import akka.actor.Actors;

public class ActorTest {

    public static void main(String[] args) {
        ActorRef myActor = Actors.actorOf(MyActor.class);
        myActor.start();
        System.out.println("My Actor started");
    }

}

解决方法:

在你的akka​​-1.0.zip文件中有scala-library.jar.尝试将其添加到构建路径.

此外,zip中还有一个lib_managed目录,其中包含更多库文件.可能还需要其中一些.

为了避免这种情况,你应该尝试maven.有一个Akka存储库:http://scalablesolutions.se/akka/repository/se/scalablesolutions/akka/

上一篇:log4j和java.lang.NoClassDefFoundError:org / apache / log4j / Layout


下一篇:java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream