202

Similar problem like one posted on invalid target release: 1.7 but after following the blog my problem is still unresolved.

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project hm_app: Fatal error compiling: invalid target release: 1.8 -> [Help 1]

I was following as tutorial when I faced this problem.

abt java & mvn

C:\mvn>echo %JAVA_HOME% 
C:\mvn>echo %JRE_HOME%
C:\mvn>echo %MAVEN_HOME% yields

outputs

C:\Program Files\Java\jdk1.7.0_51
C:\Program Files\Java\jre7
C:\apache-maven-3.0.4
Community
  • 1
  • 1
rvd
  • 2,214
  • 3
  • 13
  • 14
  • 5
    ...and you're sure you have Java 8 installed somewhere? – Makoto Feb 03 '15 at 05:10
  • i have x86 , 64 java 7 and no java 8 – rvd Feb 03 '15 at 05:16
  • 4
    Okay. So why are you trying to compile Java 8 code without a Java 8 compiler? – Makoto Feb 03 '15 at 05:17
  • @Makoto ty for looking into my problem installing java 8 solved it .... ty man :) – rvd Feb 03 '15 at 05:49
  • @Makoto Finally i got what u were saying "Okay. So why are you trying to compile Java 8 code without a Java 8 compiler?" so i just changed in my pom.xml file java.version>1.8 to java.version>1.7 and i could use mvn compile on java7 too – rvd Feb 04 '15 at 05:43

22 Answers22

315

You have set your %JAVA_HOME to jdk 1.7, but you are trying to compile using 1.8. Install jdk 1.8 and make sure your %JAVA_HOME points to that or drop the target release to 1.7.

invalid target release: 1.8

The target release refers to the jdk version.

Zavior
  • 6,412
  • 2
  • 29
  • 38
  • 7
    one more possible solution in my case is edit pom.xml file java.version>1.8 to java.version>1.7 – rvd Feb 04 '15 at 05:44
  • 1
    That is specifically where maven looks for the java version. So effectively you changed the java version to one you had installed on your %JAVA_HOME. – Zavior Feb 04 '15 at 08:42
  • 3
    On a my mac, I do export JAVA_HOME=`/usr/libexec/java_home -v 1.8` to set the JAVA_HOME to the proper location for 1.8 JDK. Supply your favorite version in place of 1.8. (There are backticks after = and after 8). – bruce szalwinski Aug 28 '15 at 16:09
  • Yes, right u got me. I changed it to compile another legacy program which only supports 1.7 yesterday :D Thanks! – Davut Gürbüz Dec 31 '19 at 13:19
44

Put the value in the plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

The error was use:

<source>${java.version}</source>
<target>${java.version}</target>
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
Camila Macedo
  • 857
  • 7
  • 10
25

In my case the maven "Run configuration" was using the wrong JRE (1.7). Be sure to check Run -> Run Configurations -> (Tab) JRE to be some jdk1.8.x.

Roland Ettinger
  • 2,615
  • 3
  • 23
  • 24
  • that is true, so we have following things to check next time if this error comes: JAVA_HOME, BUILD_Configuration, RUN_Configuration – Farhan Jul 26 '18 at 07:30
16

The issue was resolved as I was having a JDK pointing to 1.7 and JRE pointing to 1.8. Check in the command prompt by typing

java -version

and

javac -version

Both should be same.  

Community
  • 1
  • 1
Bikramjit Saha
  • 161
  • 1
  • 2
6

For eclipse here is how I solved my problem:

  1. Preferences --> Compiler --> Compiler Complainer Level (Change to 1.8) enter image description here

  2. Perferences --> Installed JREs --> select JAVA SE 8 1.8 enter image description here

  3. Rebuild via maven using Run as maven build.

It shouldn't show you the invalid target error anymore.
Note: I didn't have to set or change any other variables in my terminal. Hope this helps.

РАВИ
  • 11,467
  • 6
  • 31
  • 40
5

Putting that in your .profile will dynamically take care of your $JAVA_HOME

export JAVA_HOME=$(/usr/libexec/java_home)

Close your shell afterwards, open a new one and test with

echo $JAVA_HOME

It should display something like

/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home

If not, remove any other assignments of JAVA_HOME in your startup scripts. Remember, that these startup scripts start with a . so they are hidden and won't be included when using * wildcard, e.g. if you want to grep all files of your home directory, you need to:

grep -s JAVA_HOME ~/.* --exclude=.bash_history
lilalinux
  • 2,903
  • 3
  • 43
  • 53
4

I faced this issue deploying on Dokku, for some reason it was choosing JDK 1.7

Creating a system.properties file and setting java.runtime.version=1.8 solved the issue. Dokku now uses Java version 8. Choosing a JDK on Heroku

I Never had to do it before...

3

As mentioned by Camila Macedo - you have to explicitly point the java version for compiler-plugin. For spring boot you can do that by next property:

  <properties>
    <java.version>1.8</java.version>
    <maven.compiler.release>8</maven.compiler.release>
  </properties>
M. Gryshenko
  • 31
  • 1
  • 3
3

I have similar problem when I was trying to deploy my spring boot project to heroku, although my problem is invalid target release: 11. I solved it by adding a new file system.properties and the content added to the file is java.runtime.version=11 (For me I am using JDK 11).

Anonymous
  • 738
  • 4
  • 14
  • 36
Jason Huang
  • 179
  • 1
  • 3
2

Using IntelliJ I just had to install another (higher) JDK Version. After restarting IDE, everything worked and even all dependencies were solved.

Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • 1
    Restarting intelliJ was the key for me. I updated the environment variable of JAVA_HOME to point to different java version, but it appears the system variable doesn't refresh in existing CMD session. Possibly it would be enough to just create new Terminal session. – Ubeogesh Dec 09 '19 at 12:23
1

The problem I was facing was that I was able to make a maven build from the command prompt but not from Eclipse.What worked for me in eclipse is that I changed the run configuration to point to JRE folder inside of JDK rather than leaving it in JDK folder only as per the standard.This solution may work for you as well but try this if and only if all the java paths are correct, java and javac are showing the same version as present in the target of pom.xml.

Vaibhav Gupta
  • 638
  • 4
  • 12
1

On Windows machine you can temporarily set Java version.
For example, to change the version to Java 8, run this command on cmd:

set JAVA_HOME=C:\\...\jdk1.8.0_65
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
1

In my case (IntelliJ), I needed to check if I had the right Runner for maven:

  1. Preferences
  2. Build, Execution, Deployment
  3. Maven -> Runner

Preferences

Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
0

This question wasn't asking explicitly about Docker, but I received the same error when I had a pom.xml file that was targeting 1.9...

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.9</maven.compiler.source>
  <maven.compiler.target>1.9</maven.compiler.target>
</properties>

... but then tried to run tests against a Docker container by specifying "maven" by itself.

docker run -t --rm -v m2_repository:/root/.m2/repository -v $(pwd):/work -w /work maven mvn -e test

For me, the fix was to target the exact version I needed.

docker run -t --rm -v m2_repository:/root/.m2/repository -v $(pwd):/work -w /work maven:3.5.2-jdk-9 mvn test

(You can learn more here.)

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

Do a force Maven Update which will bring the compatible 1.8 Jar Versions and then while building, update the JRE Versions in Execute environment to 1.8 from Run Configurations and hit RUN

Sandy
  • 1
  • 1
0

If you are using Eclipse IDE then go inside Window menu and select preferences and there you search for installed JREs and select the JRE you need to build the project

0

NETBEANS Error: Failed to execute goal org.apache.maven.pluginsmaven-compiler-plugin3.1compile (default-compile) on project doit Fatal error compiling invalid target release 11 - [Help 1]

How I solved my problem: .Right click on the project. .Go to Properties. .Click on Source. .Go to Source/Binary Format and change to the latest version

In my case (1.8)

Kyle
  • 1
  • 1
0

First, if you are using eclipse, please check Build Path -> Configure Build Path -> Java Build Path, see which java version your project is using. I recommend using Java 1.8. Second, check the pom.xml file. ctrl + F to search the text "java.version" => change it to 1.8. Third install Java 1.8 and then change your JAVE_HOME path to point to Java 1.8 folder.

0

Verify your jdk version in your pom.xml and environmental variables are same or not. if not make it same.

At pom.xml ->

<properties>
        <java.version>11</java.version>
</properties>

At environmental variables -> JAVA_HOME = C:\Program Files\Java\jdk-11.0.13

0

In my case bumping the version of the maven-compiler-plugin from 3.8.1 to 3.10.1 solved the issue:

<plugin>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.10.1</version>
   <configuration>
   <release>${maven.compiler.release}</release>
   </configuration>
</plugin>

where ${maven.compiler.release} = your java version

tonnoz
  • 454
  • 4
  • 12
0

I had a similar failure when I tried to build a Maven project in Netbeans. My pom file looks like this:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <exec.mainClass>com.example.Main</exec.mainClass>
    </properties>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
            <source>17</source>
            <target>17</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin> 
        ...

But Java platform for the project was set to 1.8 (as it is the default Java platform in my Netbeans configuration).

Solution: Right-click on project Properties->Build->Compile->Java Platform-> choose JDK 17 from dropdown.

If the dropdown does not have the required Java platform, it should be downloaded, installed, and then configured thru Tools->Java platforms.

Yuriy N.
  • 4,936
  • 2
  • 38
  • 31
-4

What worked in my case is this:

I opened the pom.xml and replaced the one of the plug-ins as below.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
    </configuration>
</plugin>

Before I edited the source and target tags both had 1.8, I changed that to 1.7 and it worked.

nkr
  • 3,026
  • 7
  • 31
  • 39