Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/StreamingContext

It look like the class StreamingContext is not found in the following code.

import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}
object Exemple {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("Exemple")
    val sc = new SparkContext(conf)
    val ssc = new StreamingContext(sc, Seconds(2)) //this line throws error

  }
}

here is the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/streaming/StreamingContext
    at Exemple$.main(Exemple.scala:16)
    at Exemple.main(Exemple.scala)
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.StreamingContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

Process finished with exit code 1

I use the following build.sbt file:

name := "exemple"

version := "1.0.0"

scalaVersion := "2.11.11"

// https://mvnrepository.com/artifact/org.apache.spark/spark-sql
libraryDependencies += "org.apache.spark" %% "spark-sql" % "2.2.0"
// https://mvnrepository.com/artifact/org.apache.spark/spark-streaming
libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.2.0" % "provided"
// https://mvnrepository.com/artifact/org.apache.spark/spark-streaming-kafka-0-10
libraryDependencies += "org.apache.spark" %% "spark-streaming-kafka-0-10" % "2.2.0"

I run the Exemple class using intellij Run button and I get the error. in sbt shell it work fine. into my dependecies'module, I can find spark dependencies. The code compile in intellij. And I can see in the External Libraries spark dependies (inside the left project panel). Do you have any idea. It seem not complicated.

enter image description here

like image 752
a.moussa Avatar asked Sep 15 '25 09:09

a.moussa


1 Answers

Please remove provided term from spark-streaming library.

libraryDependencies += "org.apache.spark" %% "spark-streaming" % "2.2.0" 

After the changes, still having dependency issues further, exclude the duplicate jars.

 "org.apache.spark" %% "spark-streaming-kafka-0-10" % "2.2.0" excludeAll(
      ExclusionRule(organization = "org.spark-project.spark", name = "unused"),
      ExclusionRule(organization = "org.apache.spark", name = "spark-streaming"),
      ExclusionRule(organization = "org.apache.hadoop")
    ),

Hope this helps.

Thanks Ravi

like image 118
Ravikumar Avatar answered Sep 18 '25 09:09

Ravikumar