Maven error – invalid target release: 17
We upgraded the project from Java 11 to Java 17, and mvn test
hits the Fatal error compiling: error: invalid target release: 17
<properties>
<java.version>17</java.version>
</properties>
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.
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.
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.
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.
export JAVA_HOME=$(/usr/libexec/java_home -v 11)
We upgraded the JAVA_HOME
to Java 17.
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
.
source ~/.zshenv
2.3 Recheck Maven’s Java version.
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.
My JAVA_HOME is pointing to 17; which is the same version Maven is targeting. Yet I still get an “Invalid target release” error. So what are other potential causes/fixes?
Same issue
Even though JAVA_HOME and M2_HOME are both JDK 17, I still get the same error.
Do I need to restart IntelliJ IDEA or my machine after changing the environment variables for Java version ?
Same error is reported if you specify higher version of Java then compiler version
thanks so much dude, helps a lot
While this does help, one may wish to explore addressing this using Profiles in maven. This answer in stackoverflow is a quick guide.
Thanks a lot. You helped me.
./mvnw clean install finally worked thanks to u sir
helped my big time, i was stuck for few days on this
The only thing that worked! all the solutions mention changing settings on intellij but this is the one that made it for me. Thanks a lot!
I changed the JRE path copying from path variable and it worked