Last Updated:

Resolving the "invalid target release" error - Java

Cases in which you may encounter the error "Invalid target release":

 

Maven error — Fatal error compiling: invalid target release

 

You want to build a Maven project, you have a newer version of Java in your system, and you have an older version installed on your system:

pom.xml

invalid target release

Fatal error compiling: invalid target release when building a project by maven

In this case, JDK 8 is installed on the system, and Java 12 is specified in the system.

pom.xml

Solution to the problem

In this case, you need to either upgrade your JDK to a newer one or downgrade the required version in the file to the one you have installed on your system.pom.xml

"Invalid target release" error when building in the IDE

 

So, you have a project open in your favorite IDE. Take IntelliJ Idea as an example. Perhaps you created a project in Idea by importing or , or perhaps you created this project from scratch.

pom.xmlbuild.gradle

On the operating system, you have JDK 8 installed and this version is connected in the Idea settings. However, the Idea project settings or file specify JDK version 12 (or later).

pom.xml

In this case, when you try to build the project, you will only see an error about the wrong version:

invalid target release

Invalid target release when building in the IDE
Error:java: invalid source release: 12

Solution to the problem

 

The first solution is to install a newer version of the JDK and specify it in the IntelliJ Idea settings

The second solution to this error is to downgrade the version in the project properties or to / . Note: This is usually a less acceptable option than installing a later JDK. It is preferable to install a newer JDK on the system.

pom.xmlbuild.gradle

"Invalid target release" error in the compiler

A much less common, but perfectly acceptable, option is that you compile the sources by directly invoking the Java compiler. The cause of the problem here is the same – the older compiler does not know about the existence of a newer version of Java, for which you want to compile the byte code.

javac

Solution to the problem

First, check the compiler version:

javac -version

Then check which version you specify using the .

-target

If the compiler version is lower than the requested version, you must either pass a lower version to the compiler options or update the JDK.

"Invalid target release" error when specifying a non-existent version of Java

 

It is quite possible that you have a non-existent version of Java specified in the project build script. You just need to make sure that you have specified the correct version of Java.