Search code examples
javascalasbtjacocoscalatest

sbt-jacoco plugin does not work with Java11 and throws instrumenting class error


I have upgraded the java version of a scala application which is using sbt-jacoco plugin to run and check the scalatest coverage. After upgrading Java8 to Java11 I started getting an error which is mentioned below but it was working fine with Java8.

[error] java.io.IOException: Error while instrumenting class MonthTimeTrigger.class.
[error]         at org.jacoco.core.instr.Instrumenter.instrumentError(Instrumenter.java:166)
[error]         at org.jacoco.core.instr.Instrumenter.instrument(Instrumenter.java:117)
[error]         at org.jacoco.core.instr.Instrumenter.instrument(Instrumenter.java:136)

Note: The above error I was getting whenever I used to use any Java code within the Scala application and try to run sbt jacoco.

current scala application environment

  • SBT version: 1.2.8
  • SBT-Jacoco plugin version: 3.1.0
  • scalatest version: 3.0.0
  • Scala version(s): 2.12.11
  • Java version: 11

Solution

  • I have only found the solution after some research. And the problem is with the sbt-jacoco plugin itself as it does not support java10 onwards.

    They (sbt-jacoco) did some workaround for this problem and probably will get these things fixed in the 4.0 release.

    For now, the workaround is to override some of the jacoco dependencies in either plugins.sbt or build.sbt:

    // plugins.sbt
    val jacocoVersion = "0.8.5"
    
    dependencyOverrides ++= Seq(
      "org.jacoco" % "org.jacoco.core" % jacocoVersion,
      "org.jacoco" % "org.jacoco.report" % jacocoVersion,
      "org.jacoco" % "org.jacoco.agent" % jacocoVersion)
    
    // build.sbt
    val jacocoVersion = "0.8.5"
    
    dependencyOverrides ++= Seq(
      "org.jacoco" % "org.jacoco.core" % jacocoVersion % Test,
      "org.jacoco" % "org.jacoco.report" % jacocoVersion % Test,
      "org.jacoco" % "org.jacoco.agent" % jacocoVersion % Test)
    

    Note: Even if you override only org.jacoco.core would work.