I made a jar file for a Kafka Streams application coded in Scala using both IntelliJ IDEA itself and then SBT. Following are the problems I have been facing and so far I haven't succeeded:
First, I made a jar file using IntelliJ IDEA. Upon running the Jar file, it gave the following error:
java.lang.ClassNotFoundException: Main ...
Following are the steps I took to make the Jar file in IntelliJ IDEA:
- Selected Project Structure from the file menu.
- Went into the 'Artifacts' tab.
- Clicked on the + button.
- Selected the main class and the META-INF directory location
- Added all the Scala libraries and dependencies (and made sure the META-INF dir was included)
- Applied the settings and built the artifact.
- Run the Jar using Scala command. Gave the aforementioned error.
After that I tried making the Jar file using SBT. I made an assembly.sbt file inside the project directory and then I reimported the project. After that, I executed the following commands:
- sbt clean
- sbt package
- sbt assembly
It made a Jar file. This time, the problem was that it wasn't picking some of the Kafka libraries randomly. Sometimes, it'll throw, for example, that KafkaStreams not found. Then I'll try to rebuild the jar and the new jar would then throw StreamsBuilder not found. I kept on trying and to sum up, following are some of the errors I remember:
NoClassDefFoundException ..../KafkaStreams
NoClassDefFoundException ..../StreamsBuilder etc.
Here '...' means the full package name/path.
build.sbt:
name := "StreamsNew"
version := "0.1"
scalaVersion := "2.12.7"
mainClass := Some("src/main/scala/StreamsDemo/Main.scala")
libraryDependencies ++= Seq(
//"org.slf4j" % "slf4j-simple" % "1.7.28",
"org.apache.kafka" % "kafka-streams" % "2.3.0",
"org.apache.kafka" %% "kafka-streams-scala" % "2.3.0",
)
assembly.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10" )
resolvers += Resolver.url("bintray-sbt-plugins", url("http://dl.bintray.com/sbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns)
Scala version: 2.12.7
SBT Version: 1.6.3