java.lang.NoClassDefFoundError:com / typesafe / config / Config在STS中运行akka-quickstart-java时

时间:2017-06-20 06:57:00

标签: java maven akka

我正在尝试运行http://developer.lightbend.com/guides/akka-quickstart-java/ 在Spring Tool Suite中提供项目(eclipse)

文件如下:

1.AkkaQuickstart.java

import java.io.IOException;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;

public class AkkaQuickstart {
  public static void main(String[] args) {
    final ActorSystem system = ActorSystem.create("helloakka");
    try {
      //#create-actors
      final ActorRef printerActor =
        system.actorOf(Printer.props(), "printerActor");
      final ActorRef howdyGreeter =
        system.actorOf(Greeter.props("Howdy", printerActor), "howdyGreeter");
      final ActorRef helloGreeter =
        system.actorOf(Greeter.props("Hello", printerActor), "helloGreeter");
      final ActorRef goodDayGreeter =
        system.actorOf(Greeter.props("Good day", printerActor), "goodDayGreeter");
      //#create-actors

      //#main-send-messages
      howdyGreeter.tell(new WhoToGreet("Akka"), ActorRef.noSender());
      howdyGreeter.tell(new Greet(), ActorRef.noSender());

      howdyGreeter.tell(new WhoToGreet("Lightbend"), ActorRef.noSender());
      howdyGreeter.tell(new Greet(), ActorRef.noSender());

      helloGreeter.tell(new WhoToGreet("Java"), ActorRef.noSender());
      helloGreeter.tell(new Greet(), ActorRef.noSender());

      goodDayGreeter.tell(new WhoToGreet("Play"), ActorRef.noSender());
      goodDayGreeter.tell(new Greet(), ActorRef.noSender());
      //#main-send-messages

      System.out.println(">>> Press ENTER to exit <<<");
      System.in.read();
    } catch (IOException ioe) {
    } finally {
      system.terminate();
    }
  }
}

2.Greeter.java

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;

//#greeter-messages
public class Greeter extends AbstractActor {
//#greeter-messages
  static public Props props(String message, ActorRef printerActor) {
    return Props.create(Greeter.class, () -> new Greeter(message, printerActor));
  }

  //#greeter-messages
  static public class WhoToGreet {
    public final String who;

    public WhoToGreet(String who) {
        this.who = who;
    }
  }

  static public class Greet {
    public Greet() {
    }
  }
  //#greeter-messages

  private final String message;
  private final ActorRef printerActor;
  private String greeting = "";

  public Greeter(String message, ActorRef printerActor) {
    this.message = message;
    this.printerActor = printerActor;
  }

  @Override
  public Receive createReceive() {
    return receiveBuilder()
        .match(WhoToGreet.class, wtg -> {
          this.greeting = message + ", " + wtg.who;
        })
        .match(Greet.class, x -> {
          //#greeter-send-message
          printerActor.tell(new Greeting(greeting), getSelf());
          //#greeter-send-message
        })
        .build();
  }
//#greeter-messages
}
//#greeter-messages

3.Printer.java

    import akka.actor.AbstractActor;
    import akka.actor.Props;
    import akka.event.Logging;
    import akka.event.LoggingAdapter;

    //#printer-messages
    public class Printer extends AbstractActor {
    //#printer-messages
      static public Props props() {
        return Props.create(Printer.class, () -> new Printer());
      }

      //#printer-messages
      static public class Greeting {
        public final String message;

        public Greeting(String message) {
          this.message = message;
        }
      }
      //#printer-messages

      private LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);

      public Printer() {
      }

      @Override
      public Receive createReceive() {
        return receiveBuilder()
            .match(Greeting.class, greeting -> {
                log.info(greeting.message);
            })
            .build();
      }
    //#printer-messages
    }
    //#printer-messages

4.pom.xml

<!-- #build-sample -->
        <project>
        <modelVersion>4.0.0</modelVersion>

        <groupId>hello-akka-java</groupId>
        <artifactId>app</artifactId>
        <version>1.0</version>

        <dependencies>
            <dependency>
                <groupId>com.typesafe.akka</groupId>
                <artifactId>akka-actor_2.12</artifactId>
                <version>2.5.2</version>
            </dependency>
            <dependency>
                <groupId>com.typesafe.akka</groupId>
                <artifactId>akka-testkit_2.12</artifactId>
                <version>2.5.2</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.6.0</version>
                    <configuration>
                        <executable>java</executable>
                        <arguments>
                            <argument>-classpath</argument>
                            <classpath />
                            <argument>com.lightbend.akka.sample.AkkaQuickstart</argument>
                        </arguments>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        </project>

我正在尝试运行maven build:mvn clean install: 输出是:

...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ app ---
[INFO] Surefire report directory: /Users/xxx/Downloads/akka-quickstart-java/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.lightbend.akka.sample.AkkaQuickstartTest
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.555 sec <<< FAILURE!
com.lightbend.akka.sample.AkkaQuickstartTest  Time elapsed: 0.555 sec  <<< ERROR!
java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: com/typesafe/config/Config
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:241)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:287)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:232)
    at akka.actor.ActorSystem$.apply(ActorSystem.scala:223)
    at akka.actor.ActorSystem$.create(ActorSystem.scala:158)
    at akka.actor.ActorSystem.create(ActorSystem.scala)
    at com.lightbend.akka.sample.AkkaQuickstartTest.setup(AkkaQuickstartTest.java:20)
...

我到底错过了什么?

1 个答案:

答案 0 :(得分:1)

您缺少主要的com.typesafe依赖项。在您的POM.xml中添加

<dependency>
    <groupId>com.typesafe</groupId>
    <artifactId>config</artifactId>
    <version>1.2.1</version>
</dependency>

java.lang.NoClassDefFoundError主要是由于缺少依赖项/ jar。 希望有所帮助