Enterprise Java

How to fix invalid target release: 1.7, 1.8, 1.9, or 1.10 Error in Maven Build

If you are building your Java project using Maven, maybe in Eclipse or from the command prompt by running mvn install and your build is failing with an error like “invalid target release: 1.7” or “invalid target release: 1.8” then you have come to the right place. In this article, I’ll show you the reason why this error occurs and how you can deal with these errors even with higher Java version like Java 9, 10 installed on your machine, or maybe with Java 11 in the coming month. The root cause of the problem is that you have specified a higher Java version in your pom.xml file for Maven compiler plugin then what Maven knows in your system, and that’s why it’s saying invalid target release.

A simple solution to this problem is either reduce your target version in pom.xml or install a new Java version if you want to build your project in higher version

But, the key to solving this problem is knowing that Maven picks the Java version from the JAVA_HOME variable and not from the PATH environment variable. 

This means even if you have JDK 8 installed but if your JAVA_HOME is still referring to JDK 1.7 then you will get this error.

Thankfully, you can find out that, I mean which version of Java your Maven is using. Just run the mvn -version command from the command prompt and it will print the value of JAVA_HOME variable and confirm that which version of JDK it is using to build your project.

Invalid target release: 1.7 

Recently I was building a Java project using Maven on my old machine when I encountered this error. I thought I have the latest Java version which I had and that’s why I surprised with this error.

Here is what my Maven pom.xml looks like:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

Here is an example output of mvn -version:

$ mvn -version
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-12T04:58:10+08:00
Maven home: C:\apache-maven-3.2.3
Java version: 1.6.0_37, vendor: Sun Microsystems Inc.
Java home: C:\jdk1.6.0_37\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"

You can see that Maven in my machine was using JDK 1.6.0_37 and that’s why when I was building my Maven project with JDK 1.7 as the target it was failing.

Once I updated the pom.xml to use target 1.6 the build was started working.

Because Java 7 was not required for my project, I just had to change the Maven compile plugin as follows:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

But, if you have to build your Java project with JDK 1.7 then just install and update your JAVA_HOME variable and this error will go away.

This solution is really nice as you can solve the problem immediately.

Maven Build

Invalid target release: 1.8

This error comes if your maven compiler plugin has <target> 1.8 </
target > but JAVA_HOME variable in your machine is referring to a Java version which is lower than JDK 1.8 e.g. JDK 1.6 or JDK 1.7.

The Maven file initially might look like:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

In order to solve this error either change your target to match with the Java version, your JAVA_HOME variable is referring to or install a new JDK and updated the JAVA_HOME variable.

If you don’t know then see this article to find out exact steps to change the value of JAVA_HOME environment variable in  Windows and  Linux.

Btw, If you are working in the restricted environment like in a big company where Software is deployed automatically and can’t add or edit an environment variable, you can still change them in the local shell as shown in my earlier article  How to set a specific Java version for Maven in Windows.

Maven Build

Invalid target release: 1.9

This error will come if your maven compiler plugin has < target > 1.9 </ target > but JAVA_HOME variable in your machine is referring to a Java version which is lower than JDK 1.9 e.g. JDK 1.8 or JDK 1.7.

As explained in the previous section either change the target into pom.xml or install a new JDK and updated the JAVA_HOME environment variable to point out new JDK bin directory.

Since Maven uses JAVA_HOME, it will not solve the problem until you update this environment variable, even if you have installed the correct version of Java.

Similarly, you will get Invalid target release: 1.10 if you compile using
< target > 1.10 </ target > and your JAVA_HOME is referring to JDK 9 or JDK 8.

Now you can generalize this problem and solve depending upon which version of Java you have specified in your pom.xml and which version of Java is installed on your machine and referred by the JAVA_HOME environment variable.

Other Maven articles you may like

  • What is the difference between Maven, ANT, and Jenkins? (answer)
  • How to increase the heap size of Maven? (steps)
  • How to fix Maven Eclipse Dependency search not working issue? (solution)
  • How to install Maven in Windows 10? (steps)
  • How to build a Java project using ANT? (article)
  • How to create or modify build.xm in ANT? (tutorial)
  • Top 5 Apache Maven Books for Free (books)
  • 10 Points about Maven Java Developer should know (maven)
  • 10 Maven Plugins Every Java Developers should know (plugins)
  • 6 Free Maven and Jenkins Courses for Java developers (courses)
  • How to set a specific Java version for Maven (tutorial)

Thanks for reading this article so far. If you manage to solve your problem by following these tips then please share with your friends and colleagues. If you are still facing issues then please drop a note and we may be able to solve your problem together.

Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: How to fix invalid target release: 1.7, 1.8, 1.9, or 1.10 Error in Maven Build

 

Opinions expressed by Java Code Geeks contributors are their own.

Javin Paul

I have been working in Java, FIX Tutorial and Tibco RV messaging technology from past 7 years. I am interested in writing and meeting people, reading and learning about new subjects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button