Maven 没有使用 Java11: error 消息“ Fatal error coding: void target release: 11”

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

当我尝试使用 Java8作为 Pom.xml中的 Java 版本运行应用程序时,它工作得很好。但是当我尝试用 Java11运行它时,它会抛出一个错误。

编译致命错误: 无效目标发布: 11

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

我电脑上的命令和结果如下:

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 指向 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

环境变量是:

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
187275 次浏览

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.

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:

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

Just change this

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

to

<properties>
<java.version>1.8</java.version>
</properties>

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.

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.

  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

     <properties>
    <java.version>11</java.version>
    </properties>
    
  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

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="/path/to/obtained/from/command/lib"

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.

This what helped me:

Changed from 11 to 8, works fine

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

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 "https://get.sdkman.io" | bash

Then enter in the terminal:

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

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

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