java.lang.NoClassDefFoundError: scala/Product$class
Asked Answered
T

3

37

I am new to scala and I am trying out few sample codes for testing. However I am facing some issues when I run the test code. When I run the test, I am getting an error

[trace] Stack trace suppressed: run last test:executeTests for the full output.
[error] (test:executeTests) java.lang.NoClassDefFoundError: scala/Product$class
[error] Total time: 3 s, completed Feb 27, 2017 6:57:15 PM

My code is as follows

FilterChecks.scala

    class filterChecks extends FlatSpec {

  "Filter checker passed a filename which is present in directory" should "return file name" in {
    val matchingFileName = new FileObject("match")
    val listOfFiles = List(new FileObject("random"), matchingFileName)
    val matchedFiles = new FilterChecker("match").findMatchedFiles(listOfFiles)
    assert(matchedFiles == List(matchingFileName))

  }
}

FilterChecker Class

class FilterChecker(filter : String) {

  def matches(content : String) = content.contains(filter);

  def findMatchedFiles(fileObjects : List[FileObject]) = {
    for(fileObject <- fileObjects if(matches(fileObject.name)))
      yield fileObject
  }

}

FileObject

class FileObject(val name: String) {

}

The build file is as follows:

name := "testScalaProject"

version := "1.0"

scalaVersion := "2.12.1"

// https://mvnrepository.com/artifact/org.scala-js/scalajs-test-interface_2.12
libraryDependencies ++= Seq("org.scala-js" % "scalajs-test-interface_2.12" % "0.6.14",
  "org.scalatest" % "scalatest_2.11" % "2.2.5",
  "com.novocode" % "junit-interface" % "0.11",
  "org.scala-lang" % "scala-library" % "2.12.1")

Could someone help me out in finding the issue . Thanks in advance

Tolyl answered 28/2, 2017 at 0:4 Comment(0)
H
49

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 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

Harpsichord answered 28/2, 2017 at 0:13 Comment(2)
Not working for me.. My Test Scala Version is "2.12.8" code is build successfully but when I try to use this jar on my another project it gives me above error. My other project scala version is "2.12.6" . I have set it to the same version but not succeed and still showing the Error NoClassDefFoundError: scala/Product$class Could you please help me on this that what I have missed.Judon
@ArpitJain i am facing same issue. Were you able to manage it?Prase
L
1

specifically add scala-reflect to your pom or change sbt accordingly:

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-reflect</artifactId>
    <version>${scala.version}</version>
</dependency>
Lyrism answered 1/3, 2022 at 14:29 Comment(0)
O
0

The root cause of this issue: there is difference on scala dependency version what set on scala project and scala set at environment or system path.

Project have scala version 2.12.x and scala version set at environment variable is 2.11.x.

Both should be in synch and should have same version.

Reref Screenshoot.

enter image description here

enter image description here

Ovine answered 25/10, 2023 at 11:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.