[Solved] Maven – Fatal error compiling: invalid target release

1. Problem

When building, compiling or testing a Java application with Maven, we can get an invalid target release error due to Java version mismatch.

The error indicates that the Java versions used in Maven and in the application are not compatible. The exception trace looks like this:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile 
		(default-compile) on project resilience4j: Fatal error compiling: error: invalid target release: 17 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.

2. Solution

The solution is to use the compatible Java versions in Maven and in the application.

Step 1: Find Java Version used in the Application

The Java version used in the application is given by java.version property in the pom.xml file.

<properties>
    <java.version>17</java.version>
</properties>

In is worth knowing that we can specify a JDK version number equal to or less than the Java version used by Maven. For example, if Maven is using the Java 17, then application can be compiled with Java 17, 16, 15… or any minor version.

Step 2: Find Java Version used by Maven

The Maven uses the JAVA_HOME environment variable for locating the Java version to use. So, to correctly know the Java used by Maven:

  • Either print the maven version information.
  • Or print the JAVA_HOME environment variable.
mvn -version

//or

echo %JAVA_HOME%	 	//On Windows
echo $JAVA_HOME			//On Linux or macOS

Step 3: Use Compitable Java Versions

Now we know that this is a Java version mismatch, we can install the required Java version and update the JAVA_HOME variable. The error must be gone now.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.