scala “jvm-1.8”不是“-target”的有效选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29041527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:58:09  来源:igfitidea点击:

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

javascalaintellij-ideagradlejava-8

提问by Jens Schauder

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

当我尝试制作我的项目时,IntelliJ 会抱怨以下异常。

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.

但是“gradlew 全新安装”效果很好。

The project setup is:

项目设置是:

gradle version 2.3

gradle 版本 2.3

scala 2.10 and java

Scala 2.10 和 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.

4 个模块中的 3 个使用 java 1.7(源和目标兼容性),第 4 个模块的源和目标兼容性为 1.8 并导致问题。

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

任何想法如何避免错误?(移动到 java 7 不是一个选项,升级 scala 是)

采纳答案by Alan Effrig

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

Gradle 默认使用 ant 任务来构建 Scala 代码,https://issues.scala-lang.org/browse/SI-8966 显示 jvm 1.8 直到 Scala 2.11.5 才被添加为支持的目标。

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

您可以通过添加以下 gradle 构建文件来尝试使用基于 Zinc 的编译器。

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

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

您可能还需要将锌编译器添加到您的依赖项列表中。

回答by Bohumir Zamecnik

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

在 Intellij IDEA (15 CE) 中添加这个 scalac 编译器选项:

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

构建、执行、部署->编译器-> Scala 编译器-> 默认

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

其他编译器选项:(-target:jvm-1.7为空)。

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

还有配置文件 Gradle 1, ... with -target:jvm-1.8,所以我也将它们更改为-target:jvm-1.7.

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

我使用的是 Scala 2.10、JVM 1.8、源代码兼容性 1.7。它对我有帮助。

回答by ellisbjohns

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

我在使用 IntelliJ 2017.2.5 w/ JRE 1.8.0、scala 2.10.5 和 gradle 3.3 时收到了相同的错误消息。我通过选中 IntelliJ IDE 中的“Delegate IDE build/run actions to gradle”复选框解决了我的问题

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

设置>构建、执行、部署>构建工具>Gradle>Runner

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

详细信息可以在这里找到:https: //docs.gradle.org/4.2.1/userguide/scala_plugin.html#ideaTargetVersionhttps://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.

值得注意的是,我能够在 IntelliJ(即 cmd 行)之外构建我的 gradle 项目,并且仅在尝试使用 IntelliJ IDE 构建时收到此错误。

回答by Yevgeny Krasik

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

摘自:https: //devnet.jetbrains.com/message/5523902

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

回答by sversch

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.

如果您使用 JDK 1.8 作为您的 IntelliJ 项目 SDK,则默认项目语言级别和源兼容性可能会默认设置为版本 8。这也将反映在 IntelliJ 的 Scala 编译器设置中。

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

要覆盖默认的 Java 8 语言级别,我只需要将以下内容添加到我的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

之后,让 IntelliJ IDEA 再次导入 Gradle 项目。然后,您应该通过检查以下内容来确认 IntelliJ 已获取更改: 设置 -> 构建、执行、部署 -> 编译器 -> Scala 编译器

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

查看列出的每个模块的附加编译器选项字段,并确保它包含-target:jvm-1.7,而不是-target:jvm-1.8

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

Project Settings -> Modules 下,您应该会看到Language Level现在也设置为7

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

注意sourceCompatibility在 Gradle 属性下设置属性allprojectssubprojects或使用tasks.withType(ScalaCompile)对我不起作用;必须单独指定才能提取。

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

(这对我使用 IntelliJ IDEA 2017.2.3、Gradle 4.1、Gradle Scala 插件和 Scala 版本 2.10 有效)