'Static methods in interface require -target:jvm-1.8

I'm building scala project using gradle 4.5, scala 2.11.11/2.12.4 with JDK 1.8.0_162 and it was working fine until I upgrade to scala 2.11.12. With 2.11.12 I keep getting compile error

Static methods in interface require -target:jvm-1.8

I've been trying to search in google and add couple of stuffs like

ScalaCompileOptions.metaClass.useAnt = false

Or

targetCompatibility="1.8"

but none of them fix the issue.



Solution 1:[1]

I finally managed to fix this problem.

Turns out that I have to add these two lines to Gradle

project.tasks.compileScala.scalaCompileOptions.additionalParameters = ["-target:jvm-1.8"]
project.tasks.compileTestScala.scalaCompileOptions.additionalParameters = ["-target:jvm-1.8"]

This is fixing the issue and it doesn't come back.

Solution 2:[2]

An addition to @Wins answer for those using maven with scala-maven-plugin. You need to add the following line to plugin configuration:

<addScalacArgs>-target:jvm-1.8</addScalacArgs>

Solution 3:[3]

"Static methods in interface require -target:jvm-1.8"

This compilation error clearly states that the static method of an Interface is being invoked and generally requires a Target JVM version 1.8 as the static methods are made available in Interfaces from Java 1.8 version.

But the call invocation on Java Interface's static method is done from Scala as per question statement. So there needs to be scala Wrapper around the Java Interface so that the build tool/compiler can understand it.

It works if the code is accessed this way:

/*************** Java Code ***************/
package a;
import a.A ;
interface NewInterface { 
      // static method 
    static String sayHello(String name) 
    { 
    <Statement ....> // calls other Java Class methods => say A.x(); [Assume, class A is from package 'a' and x,y,z are some static methods]
    <Statement ....> // calls other Java Class methods => say A.y();
    <Statement ....> // calls other Java Class methods => say A.z();
    return "Hello, "+name ; 
    } 

} 

/*************** Scala Wrapper ***************/
// Scala Wrapper for Java's Interface
package b
import a.A   // importing the Java class A
trait NewTrait {
    def sayHello(name: String): String = {
    <Statement ....> A.x()          // make code in scala to replicate same functionality of the static method
    <Statement ....> A.y()          // by using Java Classes.
    <Statement ....> A.z()
    "Hello, "+name
    }
}

/*************** Scala call on Static methods of Interface ***************/
// Scala Object calls Wrapper instead of Java Interface
package b
import b.NewTrait
object NewObject extends App with NewTrait {    // Wrapper has to be extended instead of calling a java's Interface static method directly.
    sayHello("James")                // Resolved: "Static methods in interface require -target:jvm-1.8"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pppery
Solution 2 noscreenname
Solution 3 keikai