Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods in interface require -target:jvm-1.8

Tags:

gradle

scala

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.

like image 254
Wins Avatar asked Mar 10 '18 20:03

Wins


People also ask

What is the need of static method in interface?

Java interface static method helps us in providing security by not allowing implementation classes to override them. We can't define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”.

What is the need of default and static methods in interfaces?

Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces. A static method is a method that is associated with the class in which it is defined rather than with any object.

Do static methods require an object?

Static methods can be accessed without having to create a new object. A static method can only use and call other static methods or static data members. It is usually used to operate on input arguments (which can always accept), perform calculation and return value.

What are the conditions on static methods?

Features of static method:Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method. It is unable to access data that is not static (instance variables).


2 Answers

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>
like image 148
noscreenname Avatar answered Oct 11 '22 18:10

noscreenname


Ok, after struggling with this problem for a few weeks and decided to post on SO, I try to fiddle further more with gradle, 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.

like image 31
Wins Avatar answered Oct 11 '22 19:10

Wins