Maven没有使用Java 11:错误消息 “致命错误编译:无效的目标版本:11”

huangapple go评论47阅读模式
英文:

Maven is not using Java 11: error message "Fatal error compiling: invalid target release: 11"

问题

我正在尝试使用Java 11编译我的项目。

当我尝试在pom.xml中将Java版本设置为Java 8并运行应用程序时,一切正常。但是当我尝试使用Java 11运行它时,它会抛出错误。

严重错误编译:无效的目标版本:11

我尝试过多种方式来修复它,如更改环境变量、更新路径和将%JAVA_HOME%指向Java 11。

以下是在我的计算机上的命令及其结果:

java -version
echo %JAVA_HOME%

输出:

java version "11.0.8" 2020-07-14 LTS
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>

当我检查Maven版本时,它显示JDK 1.8作为Java版本:

mvn --version

输出:

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

我该如何将Maven指向Java 11?或者,如果这不是问题,如何解决此问题?我不认为这个问题是invalid target release: 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

环境变量路径如下:

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\bin;
C:\Program Files\PostgreSQL\lib;
C:\Program Files\Docker Toolbox
英文:

I am trying to compile my project with Java 11.

When I try to run the application with Java 8 as the Java version in pom.xml, it works fine. But when I try to run it with Java 11, it throws an error.

> Fatal error compiling: invalid target release: 11

I've tried to fix it in various ways, like changing the environment variable, updating the path, and pointing %JAVA_HOME% to Java 11.

The commands and their results as on my computer are as follows:

java -version
echo %JAVA_HOME%

Output:

java version &quot;11.0.8&quot; 2020-07-14 LTS
C:\Program Files\Java\jdk-11.0.8

I am using IntelliJ IDEA and have made the necessary changes as suggested in this tutorial.

My pom.xml file looks something like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;parent&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
        &lt;version&gt;2.3.0.RELEASE&lt;/version&gt;
        &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
    &lt;/parent&gt;
    &lt;groupId&gt;com.example&lt;/groupId&gt;
    &lt;artifactId&gt;demo&lt;/artifactId&gt;
    &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;

    &lt;name&gt;demo&lt;/name&gt;
    &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;

    &lt;properties&gt;
        &lt;java.version&gt;11&lt;/java.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                    &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

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

&lt;/project&gt;

When I check the Maven version, it shows JDK 1.8 as the Java version:

mvn --version

Output:

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

How can I point Maven to Java 11? Or, if that is not the problem, how to solve this issue? I don't think that this question is a duplicate of invalid target release: 1.7, as I have already implemented the solutions provided in that thread.

Error after building the project:

[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 -&gt; [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

The 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\bin;
C:\Program Files\PostgreSQL\lib;
C:\Program Files\Docker Toolbox

答案1

得分: 105

似乎您在mvn.bat中设置了JAVA_HOME。它可能指向旧版本的Java(即您的情况下是8)。

尝试在第一行之前使用set JAVA_HOME=C:\path\to\jdk11,然后再调用Maven。

英文:

It seems like you're having the JAVA_HOME set in your mvn.bat. It could be pointing to the older version of Java (i.e., 8 in your case).

set JAVA_HOME=C:\path\to\jdk11. Try using it on the first line, before calling Maven.

答案2

得分: 20

我在尝试将Spring Boot应用程序部署到Heroku时遇到了这个问题。

我的JAVA_HOME已正确设置,但我仍然收到相同的错误。

但后来这对我有效:

在您的pom.xml文件中,添加或根据您自己的上下文进行调整:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
英文:

I was experiencing this issue while trying to deploy a Spring Boot application to Heroku.

My JAVA_HOME was set correctly, but I was still receiving same error.

But then this worked for me:

In your pom.xml file, add or adapt to your own context:

&lt;plugin&gt;
    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
	&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
	&lt;configuration&gt;
		&lt;source&gt;1.8&lt;/source&gt;
		&lt;target&gt;1.8&lt;/target&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;

答案3

得分: 8

如果其他人遇到相同的问题,但是使用的是Eclipse(我知道楼主是在提到IntelliJ IDEA),也要检查用于Maven的JRE:

  • 运行配置...

  • 选择您的Maven构建配置

  • 点击JRE选项卡,根据您的POM文件选择相应的JRE版本。

英文:

If other people are having this same problem, but with Eclipse (I know OP was referring to IntelliJ IDEA), also check the JRE in use for Maven:

  • Run configurations...

  • Select your Maven Build configuration

  • Click the JRE tab and choose the JRE version in accordance with your POM file.

答案4

得分: 1

我遇到了同样的问题。将JAVA_HOME环境变量设置为正确的JDK解决了我的问题。

java -version显示了正确的版本,但**echo %JAVA_HOME%**指向了错误的JDK。修复它,对我来说解决了这个问题。

英文:

I was facing the same problem. Setting JAVA_HOME environment variable to the right JDK solved my problem.

java -version was showing the right version, but echo %JAVA_HOME% pointed to the wrong JDK. Fixing it, solved this issue for me.

答案5

得分: 1

不要忘记在Eclipse IDE的Run As -> Run Configurations -> JRE选项卡中适当更改JRE版本。

Maven没有使用Java 11:错误消息 “致命错误编译:无效的目标版本:11”

英文:

And don't forget to change JRE version appropriately in the Run As --> Run Configurations --> JRE tab in Eclipse IDE..

Maven没有使用Java 11:错误消息 “致命错误编译:无效的目标版本:11”

答案6

得分: 0

我遇到了相同的问题,即使正确安装了Java版本11并设置了JAVA_HOME变量。

从AdoptOpenJDK切换到Corretto的另一个Java供应商解决了这个问题。

您可以使用SDKMAN来轻松快速地在不同的Java版本和供应商之间切换(SDKMAN会自动正确设置JAVA_HOME变量)。

通过以下方式安装SDKMAN:

curl -s "https://get.sdkman.io" | bash

然后在终端中输入:

source "$HOME/.sdkman/bin/sdkman-init.sh"

为了确保安装成功:

sdk version

安装Java 11:

sdk install java 11.0.13.8.1-amzn
sdk default java 11.0.13.8.1-amzn

检查版本、运行时和供应商:

java --version
mvn -v
英文:

I had the same problem even the correct Java version 11 and JAVA_HOME variable was set.

Switching to another Java vendor from AdoptOpenJDK to Corretto solved it.

You can use SDKMAN to change easily and quick between different Java versions and vendors. (the JAVA_HOME variable will set automatically correct by SDKMAN).

Install SDKMAN by:

curl -s &quot;https://get.sdkman.io&quot; | bash

Then enter in the terminal:

source &quot;$HOME/.sdkman/bin/sdkman-init.sh&quot;

To ensure installation was successful:

sdk version

Install Java 11:

sdk install java 11.0.13.8.1-amzn
sdk default java 11.0.13.8.1-amzn

To check the versions, runtime and vendor:

java --version
mvn -v

答案7

得分: 0

使用SDKMAN https://sdkman.io/ 是最快最简单的方法:

  1. 安装JDK 11:

    sdk install 11.0.11.hs-adpt
    
    • 使用sdk list java查看可用的JDK列表
  2. 在项目的主目录中创建一个 .sdkmanrc 文件:

    java=11.0.11.hs-adpt
    
  3. 切换JDK

    • 使用sdk env来切换到正确的JDK。

    • 要自动切换到选择的jdk版本,请在sdkman配置文件(~/.sdkman/etc/config)中启用它:

      sdkman_auto_env=true
      
英文:

The fastest and the easiest way is to use SDKMAN https://sdkman.io/:

  1. Install JDK 11 with:

    sdk install 11.0.11.hs-adpt
    
    • sdk list java for the list of available JDK
  2. Create a .sdkmanrc file in your project main directory:

    java=11.0.11.hs-adpt
    
  3. Switch JDK

    • use sdk env for switch to right JDK.

    • For automatic switch to the chosen jdk version, enable it in sdkman config file (~/.sdkman/etc/config)

      sdkman_auto_env=true
      

答案8

得分: 0

我在使用VSCode打包时遇到相同的错误。我只需要在pom.xml中的maven-compiler-plugin中添加release元素,像这样:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
    </plugins>
</build>

感谢Bing Chat帮助解决这个问题!

英文:

I was having the same error when packaging using Maven from VSCode. I just needed to add the release element to the mavin maven-compiler-plugin in the pom.xml like this:

&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.8.1&lt;/version&gt;
            &lt;configuration&gt;
                &lt;release&gt;11&lt;/release&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

Thank you to Bing Chat to nail that one!

答案9

得分: -2

首先运行以下命令:

/usr/libexec/java_home -V

这将列出您计算机上可能的所有 JAVA_HOME 路径。

您有两个选项:

将以下行添加到您的 maven.bat 文件中(项目永久性):

JAVA_HOME="/从命令中获取的路径/lib"

最好是在初始注释之后添加。

在运行 mvnw 命令之前运行以下命令:

export JAVA_HOME=/从命令中获取的路径/lib
英文:

First run

/usr/libexec/java_home -V

This will list all the possible JAVA_HOME paths possible on your machine.

You have two options:

Add the line (project permanent)

JAVA_HOME=&quot;/path/to/obtained/from/command/lib&quot;

in your maven.bat file, ideally right after the initial comments.

Run

export JAVA_HOME=/path/to/obtained/from/command/lib

Before running the mvnw command.

答案10

得分: -3

  1. 检查环境变量中的路径下的Java主目录。它应该指向你的Java安装目录下的JDK 11。

  2. pom.xml文件中检查以下内容:

<properties>
    <java.version>11</java.version>
</properties>
  1. Path中,检查%JAVA_HOME%环境变量的正确条目,并明确设置JAVA_HOME=C:\tools\java\jdk11。

  2. 检查,你的mvn不应该覆盖Java版本11。

英文:
  1. Check the Java home under path inside environment variables. It should point to JDK 11 under your Java installation directory,

  2. Check for the below inside file pom.xml

     &lt;properties&gt;
         &lt;java.version&gt;11&lt;/java.version&gt;
     &lt;/properties&gt;
    
  3. Under Path, check the correct entry for %JAVA_HOME% environment variable and explicitly set JAVA_HOME=C:\tools\java\jdk11

  4. Check, your mvn should not be overriding the Java version 11

答案11

得分: -6

这是帮助我的部分:

Maven没有使用Java 11:错误消息 “致命错误编译:无效的目标版本:11”

问题可能是你在本地使用的是8,但在你的 pom.xml 中指向了不同的版本。

英文:

This what helped me:

Maven没有使用Java 11:错误消息 “致命错误编译:无效的目标版本:11”

The issue must be you are using 8 locally, but pointing to a different version in your pom.xml.

答案12

得分: -7

只需更改此部分:

&lt;properties&gt;
    &lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;

&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;
英文:

Just change this

&lt;properties&gt;
    &lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;

to

&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

huangapple
  • 本文由 发表于 2020年7月29日 21:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63154787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定