Reputation: 15
I am getting below java error, when I am running the akka stream code: ** Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function1$class at akka.stream.Supervision$$anon$1.(Supervision.scala:57) at akka.stream.Supervision$.(Supervision.scala:57) at akka.stream.Supervision$.(Supervision.scala) at akka.stream.ActorMaterializerSettings$.apply(ActorMaterializer.scala:268) at akka.stream.ActorMaterializerSettings$.apply(ActorMaterializer.scala:258) at akka.stream.ActorMaterializer$$anonfun$1.apply(ActorMaterializer.scala:42) at akka.stream.ActorMaterializer$$anonfun$1.apply(ActorMaterializer.scala:42) at scala.Option.getOrElse(Option.scala:121) at akka.stream.ActorMaterializer$.apply(ActorMaterializer.scala:42) at akka.stream.ActorMaterializer$.create(ActorMaterializer.scala:111) at akka.stream.ActorMaterializer.create(ActorMaterializer.scala) at com.accenture.akka.stream.Sample1.main(Sample1.java:15) Caused by: java.lang.ClassNotFoundException: scala.Function1$class at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 12 more ** Java Code:
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 code:
<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>
Please tell us what I am doing wrong
Upvotes: 0
Views: 682
Reputation: 9482
Your dependencies mix libraries that have been compiled for different Scala versions. In general, Scala does not guarantee binary compatibility between minor versions, which is why the dependencies contain _2.11
or _2.12
in the artifact IDs. You need to choose one and use it consistently. In this case, since you are including scala-library
version 2.12.0
, you should use artifacts ending with _2.12
. It's also important not to mix different versions of Akka libraries, so you should use 2.5.11 for all of the Akka dependencies. I would also recommend assigning the akka-stream-testkit
dependency to the test
scope, as with junit
. Here's a corrected example:
<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>
Note that it is safe to update the patch version of scala-libary
without changing other dependencies. Currently, the latest version of Scala 2.12 is 2.12.11. Using the latest version will ensure you have all of the bug fixes:
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.11</version>
</dependency>
Similarly, the latest version of Akka 2.5 is 2.5.31. I would recommend you use that instead of 2.5.11.
I would also recommend defining akka.version
and scala.binary.version
as Maven properties, to allow you to easily change them in one place.
Putting all of the recommendations together:
<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>
Upvotes: 2