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

前端 未结 7 963
一整个雨季
一整个雨季 2021-01-01 17:07

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:          


        
相关标签:
7条回答
  • 2021-01-01 17:21

    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.

    0 讨论(0)
  • 2021-01-01 17:23

    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.

    0 讨论(0)
  • 2021-01-01 17:24

    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"
        }
    }
    
    0 讨论(0)
  • 2021-01-01 17:26

    Adding from @Alan Effrig's answer. JVM 1.8 was not added as a supported target until Scala 2.11.5.

    In my IDE, Scala version was selected as 2.11.0, updating it to latest 2.11.12 solved the issue.

    0 讨论(0)
  • 2021-01-01 17:28

    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.

    0 讨论(0)
  • 2021-01-01 17:30

    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)

    0 讨论(0)
提交回复
热议问题