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 versions 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 the 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 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 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 the JAVA_HOME variable and confirm which version of JDK it is using to build your project.
Btw, if you are starting with Java and Maven and not familiar with fundamental concepts like PATH, CLASSPATH, JAVA_HOME, etc, I suggest you to these free Java courses to learn more those and how Java works in general, and if you want to learn Maven or just refresh the basic concepts, these free Maven Course is a nice course to start with.
Here is what my Maven pom.xml looks like:
Here is an example output of mvn -version:
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:
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. Btw, if you want to learn about Maven itself, I suggest you take a look at these best Apache Maven courses for Java developers, one of the better courses to start with Maven.
The Maven file initially might look like this:
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 the JAVA_HOME environment variable in Windows and Linux.
Btw, If you are working in a 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.
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 an 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
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 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 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 the JAVA_HOME variable and confirm which version of JDK it is using to build your project.
Btw, if you are starting with Java and Maven and not familiar with fundamental concepts like PATH, CLASSPATH, JAVA_HOME, etc, I suggest you to these free Java courses to learn more those and how Java works in general, and if you want to learn Maven or just refresh the basic concepts, these free Maven Course is a nice course to start with.
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 was surprised by 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. Btw, if you want to learn about Maven itself, I suggest you take a look at these best Apache Maven courses for Java developers, one of the better courses to start with Maven.
Invalid target release: 1.8
This error comes if your maven compiler plugin has <target>1.8</target> but the JAVA_HOME variable in your machine is referring to a Java version that is lower than JDK 1.8 e.g. JDK 1.6 or JDK 1.7.The Maven file initially might look like this:
<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 the JAVA_HOME environment variable in Windows and Linux.
Btw, If you are working in a 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.
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 like 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 an 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)
- Top 5 Courses to Learn Apache Maven in-depth (courses)
- 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 Books for Java developers (books)
- How to set a specific Java version for Maven (tutorial)
No comments :
Post a Comment