Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Fix Invalid Target Release: 1.7, 1.8, 1.9, or 1.10 Error in Maven Build

DZone's Guide to

How to Fix Invalid Target Release: 1.7, 1.8, 1.9, or 1.10 Error in Maven Build

Trying to build a Java project with Maven but your build keeps failing? Here's why.

· Java Zone ·
Free Resource

Java-based (JDBC) data connectivity to SaaS, NoSQL, and Big Data. Download Now.

If you are building your Java project using Maven, 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 why this error occurs and how you can deal with these errors even with higher Java versions, like Java 9 or 10, installed on your machine. The root cause of the problem is that you have specified a higher Java version in your pom.xml file for Maven compiler plugin than what Maven knows in your system, and that's why it's saying invalid target release.

A simple solution to this problem is either to reduce your target version in pom.xml or install a new Java version if you want to build your project in a 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 that 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 that out, meaning 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 which version of the 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 had the latest Java version, which I had, and that's why I was surprised by this error.

Here is what my Maven pom.xml looked 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 the 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 the 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 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. If you want to learn about Maven itself, I suggest you take a look at the Maven Fundamentals by Bryan Hansen course on Pluralsight, one of the better courses to start with when learning Maven.

How to set Java Version for Maven in Windows?

Invalid Target Release: 1.8

This error comes if your maven compiler plugin has <target>1.8</target> but JAVA_HOMEvariable 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.

If you are working in a restricted environment, like 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.

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

Invalid Target Release: 1.9

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

As explained in the previous section, you either need to change the target into pom.xml or install a new JDK and update the JAVA_HOME environment variable to point out a 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 it 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.

Further Learning

Maven Crash Course

Maven Fundamentals by Bryan Hansen

Java Maven: 102 The Truth About Building Java Programs

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! If you manage to solve your problem by following these tips, then please share them with your friends and colleagues. And if you are still facing issues, then please drop a note below and we may be able to solve your problem together.

Connect any Java based application to your SaaS data.  Over 100+ Java-based data source connectors.

Topics:
java ,maven ,tutorial ,invalid target ,error ,fix ,code ,invalid target release

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}