Main Tutorials

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Benjamin
11 months ago

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?

Dhruvi Kaswala
3 months ago
Reply to  Benjamin

Same issue

Arif EROL
26 days ago

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

Jay
2 months ago

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

Nobody
1 year ago

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

Jord
1 year ago

thanks so much dude, helps a lot

chilli10
1 year ago

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

Aliaksandr
1 year ago

Thanks a lot. You helped me.

mvnw
1 year ago

./mvnw clean install finally worked thanks to u sir

ehsan
1 year ago

helped my big time, i was stuck for few days on this

G p
1 year ago

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!

Ashwinee Kumar
1 year ago

I changed the JRE path copying from path variable and it worked