We upgraded the project from Java 11 to Java 17, and mvn test
hits the Fatal error compiling: error: invalid target release: 17
pom.xml
<properties>
<java.version>17</java.version>
</properties>
Terminal
mvn test
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile)
on project spring-boot-hello: Fatal error compiling: error: invalid target release: 17 -> [Help 1]
The Java version is already 17.
Terminal
java -version
openjdk version "17" 2021-09-14
OpenJDK Runtime Environment Homebrew (build 17+0)
OpenJDK 64-Bit Server VM Homebrew (build 17+0, mixed mode, sharing)
1. Find out the Java version in Maven
The Maven may be using a different Java version to compile the project, and we can use mvn -version
to find out the Maven details.
Terminal
mvn -version
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/Cellar/maven/3.8.3/libexec
Java version: 11.0.10, vendor: Oracle Corporation, runtime: /usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
Default locale: en_MY, platform encoding: UTF-8
OS name: "mac os x", version: "11.6", arch: "x86_64", family: "mac"
Try print out the JAVA_HOME
environment variable; Maven finds JAVA_HOME for Java to compile.
Terminal
echo $JAVA_HOME
/usr/local/Cellar/openjdk@11/11.0.10/libexec/openjdk.jdk/Contents/Home
The Maven is still using Java 11.
2. Solution – Update JAVA_HOME
Updating the JAVA_HOME environment variable and ensuring it points to the correct JDK.
2.1 For macOS, open the ~/.zshenv
and update the JAVA_HOME
to Java 17
The JAVA_HOME
is pointing to Java 11.
~/.zshenv
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
We upgraded the JAVA_HOME
to Java 17.
~/.zshenv
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
2.2 source
the ~/.zshenv
or reopen the shell to reflect the change of the JAVA_HOME
.
Terminal
source ~/.zshenv
2.3 Recheck Maven’s Java version.
Terminal
source ~/.zshenv
2.3 Recheck Maven’s Java version.
Terminal
mvn -version
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/Cellar/maven/3.8.3/libexec
Java version: 17, vendor: Oracle Corporation, runtime: /Users/yongmookkim/Library/Java/JavaVirtualMachines/openjdk-17/Contents/Home
Default locale: en_MY, platform encoding: UTF-8
OS name: "mac os x", version: "11.6", arch: "x86_64", family: "mac"
Note
For Windows or Linux, find the
JAVA_HOME
environment variable and ensure it is pointing to the correct JDK.