Problem
You may encounter the following error when running a Maven build:
Fatal error compiling: invalid target release: <JDK_VERSION>
Solution
Below three ways to solve the problem:
- Adding the environment variable JAVA_HOME
- Using Maven on Eclipse
- Configure your pom.xml by forking the maven-compiler-plugin
1- Adding the environment variable JAVA_HOME
This problem is due to the fact that the environment variable JAVA_HOME and the target property of the maven-compiler-plugin point to a different JDK versions. The easy solution will be to fill JAVA_HOME with the correct value.
To verify that, open the pom.xml file of your project and check the target property of the maven-compiler-plugin. For example
The target property points to JDK version 1.7.
Let’s see the value of the JAVA_HOME environment variable used by Maven. Open a prompt command and execute the following command:
Output
The JAVA_HOME points to a JDK 1.6 and this is why Maven throws the exception: invalid target release. You must set the value of JAVA_HOME to the correct JDK 1.7 home directory. Below how to perform it:
On windows
- Go to System properties -> Advanced system settings -> Advanced -> environment variable and on the System variables section select JAVA_HOME and click on Edit
- Fill the form with the following
Variable name: JAVA_HOME
Variable value: <PATH_TO_JDK_HOME>
If you are using command line you can set the variable before running the build as following:
The default location in Windows is : C:\Program Files\Java\jdk1.7.x_xx
On Unix
On Mac
in ~/.profile:
2- Using Maven on Eclipse
Another solution if you are using Maven on Eclipse IDE is to set the correct JDK in your Maven configuration:
- On your Eclipse IDE, Click on Run As… -> Run Configurations…
- Select your Maven Build or create a new one
- And on the JRE tab, check in the Runtime JRE section that you are using the correct JDK. If not, click Installed JREs… and add the adequate one.
- Click Apply
3- Setting the correct compiler in the Maven-compiler-plugin
Another solution is to fork the maven-compiler-plugin and set the full path to the correct Java compiler as below: