简体   繁体   English

Java 17:java:带有--enable-preview的无效源版本7(预览语言功能仅支持版本17)

[英]Java 17: java: invalid source release 7 with --enable-preview (preview language features are only supported for release 17)

I am using IntelliJ IDEA 2021.2.3, JDK 17. I have code snippet in Java 17我正在使用 IntelliJ IDEA 2021.2.3,JDK 17。我在 Java 17 中有代码片段

pom.xm

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>function_programming17</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>function_programming17</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
package org.example;

import java.time.LocalDate;
import java.util.function.Supplier;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");

        Supplier<LocalDate> localDateSupplier = LocalDate::now;
        Supplier<LocalDate> localDateSupplier1 = () -> LocalDate.now();

        LocalDate localDate = localDateSupplier.get();
        LocalDate localDate1 = localDateSupplier1.get();

        System.out.print("localDate = ");
        System.out.println(localDate);

        System.out.print("localDate1 = ");
        System.out.println(localDate1);
    }

}

Error错误

java: invalid source release 7 with --enable-preview
  (preview language features are only supported for release 17)

在此处输入图像描述

How to fix it?如何解决?

Set

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>function_programming17</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>function_programming17</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>17</source>
                        <target>17</target>
                        <compilerArgs>
                            --enable-preview
                        </compilerArgs>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

and

在此处输入图像描述

and

在此处输入图像描述

Now we can run/debug Java code in Java language level 17 success现在我们可以在 Java 语言级别 17 中运行/调试 Java 代码成功

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:java:带有 --enable-preview 的无效源版本 11(仅版本 15 支持预览语言功能) - Error:java: invalid source release 11 with --enable-preview (preview language features are only supported for release 15) 错误:带有 --enable-preview 的无效源版本 14 - error: invalid source release 14 with --enable-preview 在无效的源代码发布级别 13 启用预览功能,只能在源代码级别启用预览 14Java(2098258) - Preview features enabled at an invalid source release level 13, preview can be enabled only at source level 14Java(2098258) java:错误:不支持发布版本 17 - java: error: release version 17 not supported IntelliJ - 无效的源版本:17 - IntelliJ - Invalid source release: 17 如何删除 Java 16 中的 --enable-preview? - How to remove --enable-preview in Java 16? 使用 corretto 17 时无法运行带有 --enable-preview 标志的 jar 文件 - Unable to run jar file with --enable-preview flag when using corretto 17 Java:无效的源版本:9 - Java: invalid source release: 9 如何使用Gradle启用Java 12预览功能? - How to enable Java 12 preview features with Gradle? GraalVM:未为 AsyncConfiguration(类文件版本 63.65535)启用预览功能。 尝试使用“--enable-preview”运行 - GraalVM: Preview features are not enabled for AsyncConfiguration (class file version 63.65535). Try running with '--enable-preview'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM