[Fixed] Fatal Error Compiling: Invalid Target Release: 11
In this short tutorial, we will explore different ways of solving fatal error compiling: invalid target release: 11.
First, we will explain the root cause behind the error. Then, we will demonstrate how to fix it in practice.
Understanding the Error
Typically, the error occurs when we compile a project with a Java/JDK version that is not compatible with the target version.
For example, trying to compile a project that uses Java 8 whereas Maven uses Java 11 will result in fatal error compiling: invalid target release: 11.
Fixing Fatal Error Compiling: Invalid Target Release: 11
Before diving deep into the details, let’s see how to reproduce the error.
First, we will assume that our project is configured with JDK 1.8.
Next, let’s specify another version in Maven. For instance, Java 11:
<properties>
<java.version>11</java.version>
</properties>
Now, let’s compile the project using the Maven command mvn compile:
C:\Users\Asus\GIT\employeeapi>mvn compile
[INFO] Scanning for projects...
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project employeeapi: Fatal error compiling: invalid target release: 11 -> [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.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
As we can see in the logs, the compilation fails because the target version Java 11 doesn’t match the project version.
Using Maven
To fix the error, we must set a Java version that is less than or equal to the version of the project.
So, let’s update Maven to use Java 8 instead. To do so, we need to edit the pom.xml file:
<properties>
<java.version>1.8</java.version>
</properties>
Alternatively, we can use maven.compiler.target to achieve the same objective:
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
That way, we tell the compiler to use 1.8 which denotes Java 8.
Please note that using maven.compiler.source is also recommended to avoid the error: java: error: invalid source release.
Now, let’s run mvn compile and see what will happen:
C:\Users\Asus\GIT\employeeapi>mvn compile
[INFO] Scanning for projects...
...
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ employeeapi ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 14 source files to C:\Users\Asus\GIT\employeeapi\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
As shown above, our project is now compiled with success.
Using IDE
Another solution would be configuring the project with a JDK version compatible with the one specified in Maven.
So, in our case, all we need to do is change the project settings to use Java 11 instead of Java 8:
Using JAVA_HOME Environment Variable
In some cases, the problem could be in the JAVA_HOME. It may denote an older or unused version.
So, to solve the issue, we need to make sure that we installed the proper JDK version:
C:\Users\Asus>java --version
java 18.0.1.1 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)
Then, we need to update the JAVA_HOME environment variable to point to the correct JDK installation:
C:\Users\Asus>echo %JAVA_HOME%
C:\Program Files\Java\jdk-18.0.1.1
Conclusion
To sum it up, we explained in detail how to solve the error: fatal error compiling: invalid target release: 11.
Along the way, we highlighted the main cause of the error. Then, we illustrated how to solve it using practical examples.