'jvm-1.8' is not a valid choice for '-

2020-02-28 00:36发布

问题:

IntelliJ complains with the following exception when I try to make my project.

Error:scalac: 'jvm-1.8' is not a valid choice for '-target'
Error:scalac: bad option: '-target:jvm-1.8'

But a 'gradlew clean install' works just fine.

The project setup is:

gradle version 2.3

scala 2.10 and java

3 of the 4 modules use java 1.7 (source and target compatibility), the 4th module has source and target compatibility 1.8 and is causing the problem.

Any ideas how I can avoid the error? (moving to java 7 is not an option, upgrading scala is)

回答1:

Gradle by default use the ant task to build Scala code, and https://issues.scala-lang.org/browse/SI-8966 shows that jvm 1.8 was not added as a supported target until Scala 2.11.5.

You can try using the Zinc based compiler with by adding the following the gradle build file.

    tasks.withType(ScalaCompile) {
      scalaCompileOptions.useAnt = false
    }

You may also need to add the zinc compiler to your dependency list.



回答2:

In Intellij IDEA (15 CE) add this scalac compiler option:

Build, Execution, Deployment -> Compiler -> Scala Compiler -> Default

Additional compiler options: -target:jvm-1.7 (was empty).

There were also profiles Gradle 1, ... with -target:jvm-1.8, so I changed them also to -target:jvm-1.7.

I'm using Scala 2.10, JVM 1.8, source compatibility 1.7. It helped in my case.



回答3:

I got the same error message using IntelliJ 2017.2.5 w/ JRE 1.8.0, scala 2.10.5 and gradle 3.3. I solved my problem by checking the "Delegate IDE build/run actions to gradle" checkbox in IntelliJ's IDE located at

Settings>Build,Execution,Deployment>Build Tools>Gradle>Runner

Details can be found here: https://docs.gradle.org/4.2.1/userguide/scala_plugin.html#ideaTargetVersion and https://www.jetbrains.com/help/idea/gradle.html

Of note, I was able to build my gradle project outside of IntelliJ (i.e. cmd line) and only received this error on attempting to build using the IntelliJ IDE.



回答4:

I was searching for a solution for this for ages, finally found it - sharing for anyone else reading this.

Taken from: https://devnet.jetbrains.com/message/5523902

subprojects {
    tasks.withType(ScalaCompile) {
        sourceCompatibility = "1.7"
        targetCompatibility = "1.7"
    }
}


回答5:

If you're using JDK 1.8 as your IntelliJ Project SDK, then the default project language level and source compatibility may be set to version 8 by default. This will also be reflected in IntelliJ's Scala compiler settings.

To override default Java 8 language level, it was only necessary for me to add the following to my build.gradle:

sourceCompatibility = 1.7

After doing that, make IntelliJ IDEA import the Gradle project again. You should then confirm that the change has been picked up by IntelliJ by checking under: Settings -> Build, Execution, Deployment -> Compiler -> Scala Compiler

Look at the Additional compiler options field for each of the modules listed and make sure that it contains -target:jvm-1.7, and not -target:jvm-1.8.

Under Project Settings -> Modules, you should see that the Language Level is now set to 7 too.

N.B. Setting the sourceCompatibility property under the Gradle properties allprojects, subprojects or using tasks.withType(ScalaCompile) did not work for me; it had to be specified separately to be picked up.

(This worked for me using IntelliJ IDEA 2017.2.3, Gradle 4.1, the Gradle Scala plugin and Scala version 2.10)