Fatal error compiling: error: invalid target release: 17

I upgrade my Java version from 15 to 17. When I try to build (mvn package) or run the project using maven, I get the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project MVCExample:
Fatal error compiling: error: invalid target release: 17 -> [Help 1]

My Java version is 17, confirmed by java -version.

  1. Accepted Answer: Update the Java version that Maven Uses
  2. Alternatively, Instruct Maven to use previous Java version for your project in pom.xml

Accepted Answer: Update the Java version that Maven Uses

This is most likely because your local Maven installation is still using the older/different Java version than what your Maven (pom.xml) is asking for. To find out what version of Java the Maven is using, run:

mvn -version

If you see Java version 15 in the output, then this means that your Maven is using the older Java version, even though you installed newer version. cCntinue with the steps below.

1. Check your JAVA_HOME environment variable

Maven uses JAVA_HOME environment variable to find the location of Java on your machine. To find what this variable is set to, run echo $JAVA_HOME. It should point to the the older Java 15 on your machine.

2. Update your JAVA_HOME environment variable

This depends on the type of the shell you are using. See this question on how to set JAVA_HOME environment variable on Mac OS.

Hope this helped!

Alternatively, Instruct Maven to use previous Java version for your project in pom.xml

If your project doesn’t need to run Java 17, you can update your pom.xml file to use the previous Java version to match the version that the Maven is using.

For a Spring Boot project, you’d do this as follows:

<properties>
    <java.version>15</java.version>
</properties>

For a regular Java project, you’d need to change as follows:

<properties>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
</properties>

Read free Software Algorithms and Computer Science Books by CodeAhoy Learn.

Introduction to Recursion and Backtracking

Sorting Algorithms

Introduction to C Programming Language

And More...

Speak Your Mind