编辑:澄清一下,就好像编译器和运行时对于Akka的哪个版本在类路径上存在分歧一样。除了编译器看到新方法但运行时引发NoSuchMethodError的情况外,在以后尝试调用ActorContext.children (编译器看到它,但JVM引发NoSuchMethod)时,我得到了类似的错误。这个问题可能比阿克卡更普遍。
我已经做了很多次mvn clean并检查了我的Scala版本。
原始问题:
版本2.2.1的Akka文件和API接口 (我所使用的版本)指出,带有构造函数参数的Actors应该构建如下:
import akka.actor.Actor
import akka.actor.Props
import akka.actor.ActorSystem
class MyActor(one: Int, two: Double, three: String) extends Actor {
def receive = {
case "test" => println("%d %g %s".format(one, two, three))
}
}
val system = ActorSystem("MyActorSystem")
val actor = system.actorOf(Props(classOf[MyActor], 1, 2.0, "three"))虽然这是编译的,但当您尝试运行它时,它会引发java.lang.NoSuchMethodError: akka.actor.Props$.apply(Ljava/lang/Class;Lscala/collection/Seq;)Lakka/actor/Props;异常。
在REPL上运行它将导致
<console>:12 error: type mismatch;
found : Class[MyActor](classOf[$MyActor])
required: () => akka.actor.Actor
val actor = system.actorOf(Props(classOf[MyActor], 1, 2.0, "three"))
^如果我接受REPL建议的改变,
val actor = system.actorOf(Props(() => new MyActor(1, 2.0, "three")))它不需要对REPL进行评论,而且使用的是
[warn] test.scala:10 method apply in object Props is deprecated: use Props.withDispatcher and friends
val actor = system.actorOf(Props(classOf[MyActor], 1, 2.0, "three"))
^编译器中的警告。文档和迁移指南(2.1.x至2.2.x)都确认了这种否定,并表示闭包技术(我正在使用的技术)导致了不可序列化的Actors。
我不想使用不受欢迎的东西,特别是因为我刚刚开始使用这个库。我不理解关于Props.withDispatcher和朋友的评论,因为我只想使用默认的dispatcher,至少现在是这样。有这样做的例子吗?
编辑: My pom.xml如下所示。我已经注释掉了所有的测试,因为没有任何版本的scalatest适用于Scala2.10.2。我已经多次清理target目录:任何地方都没有其他Scala版本的提示(而且从来没有任何其他版本的Akka)。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>plotsmanship</name>
<!-- <description>TODO</description> -->
<inceptionYear>2013</inceptionYear>
<groupId>org.plotsmanship</groupId>
<artifactId>plotsmanship</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.tools.version>2.10</scala.tools.version>
<scala.version>2.10.2</scala.version>
</properties>
<dependencies>
<!-- Real dependencies for the jar -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7R4</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.10</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-remote_2.10</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_2.10</artifactId>
<version>2.2.1</version>
</dependency>
<!-- Dependencies for testing only (resolves to junit-4.11.jar hamcrest-core-1.3.jar scalatest_2.10-2.0.M6-SNAP8.jar) -->
<!-- <dependency> -->
<!-- <groupId>junit</groupId> -->
<!-- <artifactId>junit</artifactId> -->
<!-- <version>4.11</version> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.scalatest</groupId> -->
<!-- <artifactId>scalatest_${scala.tools.version}</artifactId> -->
<!-- <version>2.0.M6-SNAP8</version> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<!-- <testSourceDirectory>src/test/scala</testSourceDirectory> -->
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.3</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<!-- <goal>testCompile</goal> -->
</goals>
<configuration>
<args>
<arg>-deprecation</arg>
<arg>-feature</arg>
<!-- <arg>-make:transitive</arg> (is an unsupported option) -->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
<recompileMode>incremental</recompileMode>
<useZincServer>true</useZincServer>
</configuration>
</execution>
</executions>
</plugin>
<!-- <plugin> -->
<!-- <groupId>org.apache.maven.plugins</groupId> -->
<!-- <artifactId>maven-surefire-plugin</artifactId> -->
<!-- <version>2.13</version> -->
<!-- <configuration> -->
<!-- <useFile>false</useFile> -->
<!-- <disableXmlReport>true</disableXmlReport> -->
<!-- <includes> -->
<!-- <include>**/*Test.*</include> -->
<!-- <include>**/*Suite.*</include> -->
<!-- </includes> -->
<!-- </configuration> -->
<!-- </plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>org.plotsmanship.Main</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>./lib</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
target/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>发布于 2013-09-14 06:18:41
如果您使用akka 2.2.x和Scala2.10.y,您可能会遇到一个问题:查看scala/lib目录,您将看到akka-actors.jar,这是akka的2.1.z版本(只需查看清单或一些典型的类)。所以如果你像这样运行你的应用程序:
scala -cp $YourLibs:akka-actors-2.2.x ...2.1Akka将首先被添加(自动),并且不会被你的类路径中的2.2覆盖。
解决方案:
不使用scala包装器,手动运行它:
java -cp $ScalaHome/lib/scala-library.jar:$AkkaHome/$Akka_2.2_jars YourMainClass我不明白为什么akka jar会随scala一起装运。
发布于 2013-09-13 21:09:30
尝试查看类路径中的库。即使您指定了运行scala -cp的类路径,scala也会自动在scala主页的lib目录中添加scala标准库和其他库。请查一下这里。
尝试在主目录中编写一段代码来打印类路径。它将帮助你找到正在装载的东西。这个一是用java编写的,但是它可以帮上忙。
我复制并粘贴了您的第一个示例,就像API中指定的那样,并使用这个build.sbt构建了它:
name := "My Project"
version := "1.0"
scalaVersion := "2.10.2"
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
libraryDependencies ++= {
Seq(
"com.typesafe.akka" %% "akka-actor" % "2.2.1",
"com.typesafe.akka" %% "akka-remote" % "2.2.1",
"com.typesafe.akka" %% "akka-testkit" % "2.2.1"
)
}它就像一种魅力:
$ sbt clean compile run
[info] Loading global plugins from ~/.sbt/plugins
...........
[success] Total time: 4 s, completed Sep 13, 2013 6:03:05 PM
[info] Running MyActor
1 2.00000 threehttps://stackoverflow.com/questions/18794709
复制相似问题