Java 如何在 IntelliJ IDEA 中从 -source 1.6 更改为 -source 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21531377/
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-08-13 09:11:41  来源:igfitidea点击:

How to change from -source 1.6 to -source 7 in IntelliJ IDEA

javaintellij-idea

提问by El Marce

I' trying to build a in IntelliJ IDEA project that is not mine and I got the following error:

我试图在 IntelliJ IDEA 中构建一个不是我的项目,但出现以下错误:

java: diamond operator is not supported in -source 1.6 (use -source 7 or higher to enable diamond operator)

java: -source 1.6 不支持菱形运算符(使用 -source 7 或更高版本启用菱形运算符)

How do I change this setting in IntelliJ IDEA?

如何在 IntelliJ IDEA 中更改此设置?

采纳答案by Ori Dar

Ctrl+Alt+Shift+S (Project Structure icon)

Ctrl+Alt+Shift+S(项目结构图标)

Then change Project language level

然后更改项目语言级别

回答by senseiwu

File -> Project Structure -> Sources -> Language level

文件 -> 项目结构 -> 来源 -> 语言级别

You will have to reload IDEA

您将不得不重新加载 IDEA

回答by Allen

I know the OP uses IntelliJ IDEA, but Android Studio is based on IntelliJ IDEA, so I wanna say one more word.

我知道 OP 使用 IntelliJ IDEA,但 Android Studio 是基于 IntelliJ IDEA,所以我想多说一句。

If you use Android Studio, command+;(for Mac) or File->Project Structure, then in the open window follow the settings:

如果您使用 Android Studio、command+;(适用于 Mac)或File->Project Structure,则在打开的窗口中进行设置:

enter image description here

在此处输入图片说明

回答by WeirdElfB0y

For me, changing the Language Level in Project Structure and restarting IDEA didn't help.

对我来说,更改项目结构中的语言级别并重新启动 IDEA 并没有帮助。

I had to edit the build.gradlein coremodule and change the source compatibility from 1.6 to 1.7:

我必须编辑build.gradleincore模块并将源兼容性从 1.6 更改为 1.7:

apply plugin: "java"

sourceCompatibility = 1.7 //changed from 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

sourceSets.main.java.srcDirs = [ "src/" ]


eclipse.project {
    name = appName + "-core"
}

Build -> Clean Project

构建 -> 清理项目

回答by Sarah Phillips

And, if you're working with a maven project, for sanity, remember to set the java version in the pom too.

而且,如果您正在使用 Maven 项目,为了理智起见,请记住也在 pom 中设置 java 版本。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

回答by NecipAllef

For me, above answers didn't work, although they helped me solve my issue. At module level build.gradledo the following:

对我来说,上述答案不起作用,尽管它们帮助我解决了我的问题。在模块级别build.gradle执行以下操作:

compileOptions {
        // I've changed below values from VERSION_1_6 to VERSION_1_7
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

回答by ketankk

File->Project structure->Project Settings->Modules->Language level

File->Project structure->Project Settings->Modules->Language level

Change level using drop down.

使用下拉菜单更改级别。

Otherwise, If you are using maven for build,

否则,如果您使用 maven 进行构建,

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
</build>