java.lang.NoClassDefFoundError: scala/Product$class

def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("wc")
    val sc = new SparkContext(conf)

    val text = sc.textFile("test.txt")
    val words = text.flatMap{line => line.split(" ")}
    val pairs = words.map{word => (word, 1)}
    val results = pairs.reduceByKey(_+_)
    val sorted = results.sortByKey(false)
    sorted.foreach{x => println(x)}
  }

以上程序运行出现如下异常:

# java.lang.NoClassDefFoundError: scala/Product$class
``
原因是Spark是基于scala 2.11编译的,而环境使用了最新的2.12的scala的编译版本;

your sbt build file is not right. your scala version is 2.12.x but you are using libraries compiled in scala version 2.11. use the sbt settings shown below

note: I changed of the version of scalatest as 2.x versions are no longer supported for 2.12 version of scala

scalaVersion := "2.12.1"

libraryDependencies ++= Seq(
"org.scala-js" %% "scalajs-test-interface" % "0.6.14",
"org.scalatest" %% "scalatest" % "3.0.1", //version changed as these the only versions supported by 2.12
"com.novocode" % "junit-interface" % "0.11",
"org.scala-lang" % "scala-library" % scalaVersion.value
)
remember to do reload, clean and compile in your sbt console to start clean compile

你可能感兴趣的:(java.lang.NoClassDefFoundError: scala/Product$class)