当前位置: 首页 > 知识库问答 >
问题:

Maven没有使用Java11-编译错误:无效的目标版本:11

常翰
2023-03-14

我正在尝试用Java11编译我的项目。

我尝试用各种方法来修复它,比如更改环境变量,更新路径,将%JAVA_HOME%指向Java11。

我的计算机上的命令及其结果如下:

> java -version
java version "11.0.8" 2020-07-14 LTS
> echo %JAVA_HOME%
C:\Program Files\Java\jdk-11.0.8

我正在使用IntelliJ IDEA,并按照本教程中的建议进行了必要的更改。

我的pom.xml看起来如下所示:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <version>1</version>
                    <projectId>businessapplication-6963d</projectId>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

mvn --version
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_241\jre

我如何将Maven指向Java11?或者,如果这不是问题所在,如何解决这一问题?我不认为这个问题是无效目标版本:1.7的重复,因为我已经实现了那个线程中提供的解决方案。

生成项目后出错

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Fatal error compiling: invalid target release: 11 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Path Environment Variable is :- 
C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;
C:\Program Files (x86)\Intel\iCLS Client\;
C:\Program Files\Intel\iCLS Client\;
C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\WINDOWS\System32\OpenSSH\;
C:\Delhi 2.0\apache-maven-3.6.1\bin;
C:\Program Files\Java\jdk-11.0.8\bin;C:\Program Files\Intel\WiFi\bin\;
C:\Program Files\Common Files\Intel\WirelessCommon\;
C:\Program Files\Git\cmd;D:\Flutter\flutter\flutter\bin;
C:\Users\Infinity97\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin;
C:\Users\Infinity97\AppData\Local\Microsoft\WindowsApps;
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2\bin;
H:\apache-maven-3.6.3\bin;
C:\Program Files\PostgreSQL\12\bin;
C:\Program Files\PostgreSQL\12\lib;
C:\Program Files\Docker Toolbox

共有1个答案

谢麒
2023-03-14

似乎您在mvn.bat中设置了java_home。它可能指向Java的旧版本(在您的例子中为8)。

set java_home=c:\path\to\jdk11在调用Maven之前,尝试在第一行使用它。

 类似资料:
  • 我们刚刚将项目从升级到。在我的机器中构建项目时,我遇到以下错误。 [错误]无法执行目标org.apache.maven.plugins:maven-compiler-plugin:3.3:在项目上编译(default-compile)异常:编译时出现致命错误:目标版本无效:1.8->[帮助1] 下面是所使用的maven编译器插件 我看了很多帖子,其中大多数都与没有为java_home配置正确的版本

  • 我试图用Java11编译我的项目。 当我尝试用pom.xml中的Java 8作为Java版本运行应用程序时,它工作得很好。但是当我尝试用Java 11运行它时,它抛出了一个错误。 编译时出现致命错误:目标版本无效:11 我试图以各种方式修复它,例如更改环境变量,更新路径以及将%JAVA_HOME%指向Java 11。 这些命令及其结果在我的计算机上如下所示: 输出: 我正在使用IntelliJ I

  • 我也尝试过更改meven插件和java编译器的版本,但不幸的是,我无法修复错误。 下面是mvn-v的输出:Apache Maven 3.6.0

  • 当我试图用maven安装一个项目时,我遇到了这个错误。我看了很多帖子说这个错误是因为maven使用了另一个版本的java,但这不是我的情况(我想),因为我运行的每个命令都说使用的版本是java 1.7。 这是终端的结果: mvn的输出-版本 Java Home:< code >/Library/Java/JavaVirtualMachines/JDK 1 . 7 . 0 _ 79 . JDK/Co

  • 问题内容: 类似的问题,例如在无效目标版本上发布的问题:1.7,但是在关注博客之后,我的问题仍然没有解决。 无法在项目hm_app上执行目标org.apache.maven.plugins:maven-compiler- plugin:3.1:compile(默认编译):致命错误编译:无效目标版本:1.8-> [帮助1] 遇到此问题时,我正在按照教程进行操作。 输出 问题答案: 您已将jdk 设置

  • 我使用Eclipse中的Spring仪表板创建项目。我也尝试过使用Spring初始化器创建它,并尝试了java的版本8和版本9,但我仍然得到了这个结果。还试图改变maven的目标。运行配置,maven构建使用JavaSE1。8,jre 1.8。0 命令: