在线程“ main”中获取Akka流代码时出错,异常java.lang.NoClassDefFoundError:scala / Function1 $ class
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在线程“ main”中获取Akka流代码时出错,异常java.lang.NoClassDefFoundError:scala / Function1 $ class相关的知识,希望对你有一定的参考价值。
当我运行akka流代码时,我遇到了Java错误:**线程“主”中的异常java.lang.NoClassDefFoundError:scala / Function1 $ class在akka.stream.Supervision $$ anon $ 1。(Supervision.scala:57)在akka.stream.Supervision $。(Supervision.scala:57)在akka.stream.Supervision $。(Supervision.scala)在akka.stream.ActorMaterializerSettings $ .apply(ActorMaterializer.scala:268)在akka.stream.ActorMaterializerSettings $ .apply(ActorMaterializer.scala:258)在akka.stream.ActorMaterializer $$ anonfun $ 1.apply(ActorMaterializer.scala:42)在akka.stream.ActorMaterializer $$ anonfun $ 1.apply(ActorMaterializer.scala:42)在scala.Option.getOrElse(Option.scala:121)在akka.stream.ActorMaterializer $ .apply(ActorMaterializer.scala:42)在akka.stream.ActorMaterializer $ .create(ActorMaterializer.scala:111)在akka.stream.ActorMaterializer.create(ActorMaterializer.scala)在com.accenture.akka.stream.Sample1.main(Sample1.java:15)引起原因:java.lang.ClassNotFoundException:scala.Function1 $ class在java.net.URLClassLoader.findClass(URLClassLoader.java:381)在java.lang.ClassLoader.loadClass(ClassLoader.java:424)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:349)在java.lang.ClassLoader.loadClass(ClassLoader.java:357)...另外12个**Java代码:
import java.io.IOException;
import java.util.Arrays;
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.ActorMaterializer;
import akka.stream.javadsl.Source;
public class Sample1 {
public static void main(String[] args) throws IOException {
final ActorSystem system = ActorSystem.create("Sys");
final ActorMaterializer materializer = ActorMaterializer.create(system);
final String text =
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, " +
"when an unknown printer took a galley of type and scrambled it to make a type " +
"specimen book.";
Source.from(Arrays.asList(text.split("\s"))).
// transform
map(e -> e.toUpperCase()).
// print to console
runForeach(System.out::println, materializer).
handle((done, failure) -> {
system.terminate();
return NotUsed.getInstance();
});
}
}
Maven代码:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.accenture.akka</groupId>
<artifactId>AkkaDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- akka dependency -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.12</artifactId>
<version>2.5.11</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_2.11</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream-testkit_2.11</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
请告诉我们我在做什么错
您的依赖项混合了为不同的Scala版本编译的库。通常,Scala不保证次要版本之间的二进制兼容性,这就是为什么依赖项在工件ID中包含_2.11
或_2.12
的原因。您需要选择一个并持续使用它。在这种情况下,由于要包括scala-library
版本2.12.0
,因此应使用以_2.12
结尾的工件。同样重要的是不要混合使用不同版本的Akka库,因此对于所有Akka依赖项都应使用2.5.11。我也建议像akka-stream-testkit
一样,将test
依赖项分配给junit
范围。这是一个更正的示例:
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.12</artifactId>
<version>2.5.11</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.12</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_2.12</artifactId>
<version>2.5.11</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream-testkit_2.12</artifactId>
<version>2.5.11</version>
<scope>test</scope>
</dependency>
注意,可以安全地更新scala-libary
的补丁程序版本,而无需更改其他依赖项。当前,Scala 2.12的最新版本是2.12.11。使用最新版本将确保您具有所有错误修复:
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.11</version>
</dependency>
同样,Akka 2.5的最新版本是2.5.31。我建议您使用它而不是2.5.11。
[我也建议将akka.version
和scala.binary.version
定义为Maven properties,以便您可以轻松地在一个地方进行更改。
将所有建议汇总在一起:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.accenture.akka</groupId>
<artifactId>AkkaDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- akka dependency -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_${scala.binary.version}</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_${scala.binary.version}</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_${scala.binary.version}</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream-testkit_${scala.binary.version}</artifactId>
<version>${akka.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.11</version>
</dependency>
</dependencies>
<properties>
<akka.version>2.5.31</akka.version>
<scala.binary.version>2.12</scala.binary.version>
</properties>
</project>
以上是关于在线程“ main”中获取Akka流代码时出错,异常java.lang.NoClassDefFoundError:scala / Function1 $ class的主要内容,如果未能解决你的问题,请参考以下文章