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

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

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.

References

12 comments on “Maven error – invalid target release: 17

  1. Even though JAVA_HOME and M2_HOME are both JDK 17, I still get the same error.

    Reply
  2. Do I need to restart IntelliJ IDEA or my machine after changing the environment variables for Java version ?

    Reply
  3. 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?

    Reply
  4. Same error is reported if you specify higher version of Java then compiler version

    Reply
  5. While this does help, one may wish to explore addressing this using Profiles in maven. This answer in stackoverflow is a quick guide.

    Reply
  6. 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!

    Reply
  7. I changed the JRE path copying from path variable and it worked

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *