解决Maven编译出错——不再支持源选项 5,请使用 8 或更高版本。

问题

在idea中新建了空maven模组后,尝试启动它默认提供的主方法,编译出错提示:

[INFO] 
[INFO] ------------------< org.rhythm.test:classloader-test >------------------
[INFO] Building classloader-test 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ classloader-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ classloader-test ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to F:\Projects\JavaProjects\JVM_study\classloader-test\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] 不再支持源选项 5。请使用 8 或更高版本。
[ERROR] 不再支持目标选项 5。请使用 8 或更高版本。

解决方法

1.配置pom.xml

新建的maven工程的pom.xml文件中若不包含下面的代码片段,需要手动添加,以确保 Maven 项目使用 Java 21 版本进行编译(设置为你自己的Java版本),并且源代码的字符编码为 UTF-8。

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version> <!-- 使用最新版本 -->
                <configuration>
                    <source>21</source> <!-- 设置源代码版本为 Java 21 -->
                    <target>21</target> <!-- 设置目标平台版本为 Java 21 -->
                </configuration>
            </plugin>
        </plugins>
    </build>

2.检查Maven的JRE版本

设置使用项目的JDK版本

image

3.检查模组的SDK版本

设置使用项目的SDK版本

image

image

解决问题

image

image

posted @ 2024-08-22 09:19  AnUpdatingHam  阅读(45)  评论(0编辑  收藏  举报