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
1 2 3 4 5 6 7 8 9 10 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> |
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:
1 |
mvn –version |
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:
1 |
set JAVA_HOME=<PATH_TO_JDK_HOME> |
The default location in Windows is : C:\Program Files\Java\jdk1.7.x_xx
On Unix
1 |
export JAVA_HOME=/usr/java/jdk1.7.0_79/bin/java |
On Mac
in ~/.profile:
1 |
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7) |
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:
1 2 3 4 5 6 7 8 9 10 11 |
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <verbose>true</verbose> <fork>true</fork> <executable>C:\Program Files\Java\jdk1.7.0_79\bin\javac</executable> </configuration> </plugin> |