97

I am trying to compile my project with Java 11.

When I try to run the application with Java 8 as the Java version in pom.xml, it works fine. But when I try to run it with Java 11, it throws an error.

Fatal error compiling: invalid target release: 11

I've tried to fix it in various ways, like changing the environment variable, updating the path, and pointing %JAVA_HOME% to Java 11.

The commands and their results as on my computer are as follows:

java -version
echo %JAVA_HOME%

Output:

java version "11.0.8" 2020-07-14 LTS
C:\Program Files\Java\jdk-11.0.8

I am using IntelliJ IDEA and have made the necessary changes as suggested in this tutorial.

My pom.xml file looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <version>1</version>
                    <projectId>businessapplication-6963d</projectId>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

When I check the Maven version, it shows JDK 1.8 as the Java version:

mvn --version

Output:

Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_241\jre

How can I point Maven to Java 11? Or, if that is not the problem, how to solve this issue? I don't think that this question is a duplicate of invalid target release: 1.7, as I have already implemented the solutions provided in that thread.

Error after building the project:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project demo: Fatal error compiling: invalid target release: 11 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

The path environment variable is:

C:\Program Files (x86)\Common Files\Intel\Shared Files\cpp\bin\Intel64;
C:\Program Files (x86)\Intel\iCLS Client\;
C:\Program Files\Intel\iCLS Client\;
C:\WINDOWS\system32;C:\WINDOWS;
C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\WINDOWS\System32\OpenSSH\;
C:\Delhi 2.0\apache-maven-3.6.1\bin;
C:\Program Files\Java\jdk-11.0.8\bin;C:\Program Files\Intel\WiFi\bin\;
C:\Program Files\Common Files\Intel\WirelessCommon\;
C:\Program Files\Git\cmd;D:\Flutter\flutter\flutter\bin;
C:\Users\Infinity97\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin;
C:\Users\Infinity97\AppData\Local\Microsoft\WindowsApps;
C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.2\bin;
H:\apache-maven-3.6.3\bin;
C:\Program Files\PostgreSQL\12\bin;
C:\Program Files\PostgreSQL\12\lib;
C:\Program Files\Docker Toolbox
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ayush Surana
  • 1,776
  • 1
  • 12
  • 16
  • Where do you get the error in IntelliJ or in the console? – Simon Martinelli Jul 29 '20 at 13:35
  • Once I ran mvn clean install -DskipTests on the terminal. – Ayush Surana Jul 29 '20 at 13:35
  • 1
    And in terminal JAVA_HOME is pointing to JDK 11? – Simon Martinelli Jul 29 '20 at 13:38
  • Yes as written on the question. – Ayush Surana Jul 29 '20 at 13:39
  • And what Maven version are you using? Did you install this and has it any settings.xml configuration? – Simon Martinelli Jul 29 '20 at 13:40
  • Do you have any other Java build in your Path environment variable? – Giorgi Tsiklauri Jul 29 '20 at 13:45
  • What is javac -version? Add %JAVA_HOME%\bin as the first value in path env variable. Seems you have multiple java version on the system. IN your path variable do u have oracle DB installed? Share your path variable output. – Amit Vyas Jul 29 '20 at 13:47
  • Updated the question to have the full Error I can see. maven version is 3.6.3 I have a project where I use JAVA 8. But presently I have only Java 11 in my Path Env Variable. – Ayush Surana Jul 29 '20 at 13:49
  • Please print the output of the Path env. variable. And print javac -version output too.. Can you run the on command prompt just use mvn -e clean install – Amit Vyas Jul 29 '20 at 13:51
  • javac -version is javac 11.0.8 – Ayush Surana Jul 29 '20 at 13:53
  • Updated Path Environment Variable information to the Question. – Ayush Surana Jul 29 '20 at 13:55
  • Amit Vyas. What do you mean by adding %JAVA_HOME%\bin in my path Variable as the first entry to the path variable. Does heirarchy matter as I already have it in the Path Variable. – Ayush Surana Jul 29 '20 at 14:01
  • @procrastinator How do I set JAVA_HOME in mvn.bat? – Ayush Surana Jul 29 '20 at 14:01
  • In your case you have correct. What is your M2_HOME? And Why you have 2 maven entries in D and H drive? – Amit Vyas Jul 29 '20 at 14:05
  • 1
    You always do like create JAVA_HOME and set java without bin before the home path and refer the %JAVA_HOME%\bin as an entry in the path. No hard coding of the java path in the PATH variable. Then have M2_HOME and add a maven path without a bin folder value. In PATH variable set %M2_HOME%\bin as value. Then open new command prompt and run java -version , javac -version mvn -verion and mvn -e clean install. As @procrastinator make sure you don't have a hardcoded path in mvn.bat file. Delete all hardcoding from the PATH variable for java and maven home before doing above steps. – Amit Vyas Jul 29 '20 at 14:10
  • 2
    @procrastinator Updating JAVA_HOME in mvn.cmd(mvn.bat is mvn.cmd in maven 3.5 and above) worked. Can you post this as an answer so that I can accept it as the answer. Thank you so much!!! – Ayush Surana Jul 29 '20 at 14:13
  • @AmitVyas Thanks for the guidance. Will update my Path variables as the norm. Thanks so much for the effort. – Ayush Surana Jul 29 '20 at 14:15
  • @AyushSurana for troubleshooting it is oki to set JAVA Home explicit in the maven bat/shell file but you should not. Clean up your path/java home/maven home and everything should work seamlessly. Avoid setting explicit values in the maven as if u will try jdk 8 or 13 or 14 it will fail. Changing JAVA_HOME for nay value will make all dependent apps to work seamlessly. – Amit Vyas Jul 29 '20 at 14:25
  • A similar (and solved!) question for Heroku's Maven JDK: https://stackoverflow.com/questions/60817038/why-when-i-try-push-my-spring-boot-app-to-heroku-it-return-fatal-error-compilin – Eugene Gr. Philippov Apr 09 '21 at 18:37

12 Answers12

105

It seems like you're having the JAVA_HOME set in your mvn.bat. It could be pointing to the older version of Java (i.e., 8 in your case).

set JAVA_HOME=C:\path\to\jdk11. Try using it on the first line, before calling Maven.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
  • what if my `mvn.bat` is using `set "JAVACMD=%JAVA_HOME%\bin\java.exe"` – G. Ciardini Jul 06 '21 at 15:45
  • @G.Ciardini in that case, try checking the variable defined as `%JAVA_HOME%` and updating it. – Procrastinator Jul 07 '21 at 06:38
  • my `%JAVA_HOME%` and `java -version` are using jdk12.. so I have to use exactly jdk11? – G. Ciardini Jul 07 '21 at 07:09
  • If you want to use `jdk11` in your project then yes. Or else `jdk12`. – Procrastinator Jul 07 '21 at 10:23
  • @G.Ciardini, If you have JDK12 installed, it will compile JDK11 code. When you asked **Do I have to use exactly jdk11?**, the answer is no. As long as you compile with JDK11 **or higher** you can compile code written using the Java 11 syntax. – PatS Oct 21 '22 at 18:02
20

I was experiencing this issue while trying to deploy a Spring Boot application to Heroku.

My JAVA_HOME was set correctly, but I was still receiving same error.

But then this worked for me:

In your pom.xml file, add or adapt to your own context:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mercuriat
  • 421
  • 3
  • 4
8

If other people are having this same problem, but with Eclipse (I know OP was referring to IntelliJ IDEA), also check the JRE in use for Maven:

  • Run configurations...
  • Select your Maven Build configuration
  • Click the JRE tab and choose the JRE version in accordance with your POM file.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pierre C
  • 2,920
  • 1
  • 35
  • 35
1

I was facing the same problem. Setting JAVA_HOME environment variable to the right JDK solved my problem.

java -version was showing the right version, but echo %JAVA_HOME% pointed to the wrong JDK. Fixing it, solved this issue for me.

thelearner
  • 85
  • 1
  • 13
  • 1
    this is a duplicate of the answer that is already accepted on this question. – Jad Apr 16 '21 at 14:19
  • Hi Jad, I added this comment because it was written to to check for JAVA_HOME, and I was checking JAVA_VERSION. SO, I mentioned about both in one comment. Please check all the answers above. – thelearner Apr 17 '21 at 02:08
1

And don't forget to change JRE version appropriately in the Run As --> Run Configurations --> JRE tab in Eclipse IDE..

Run Configurations - JRE Settings

amitsriv99
  • 113
  • 1
  • 4
0

I had the same problem even the correct Java version 11 and JAVA_HOME variable was set.

Switching to another Java vendor from AdoptOpenJDK to Corretto solved it.

You can use SDKMAN to change easily and quick between different Java versions and vendors. (the JAVA_HOME variable will set automatically correct by SDKMAN).

Install SDKMAN by:

curl -s "https://get.sdkman.io" | bash

Then enter in the terminal:

source "$HOME/.sdkman/bin/sdkman-init.sh"

To ensure installation was successful:

sdk version

Install Java 11:

sdk install java 11.0.13.8.1-amzn
sdk default java 11.0.13.8.1-amzn

To check the versions, runtime and vendor:

java --version
mvn -v
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kff7
  • 69
  • 2
  • 5
0

The fastest and the easiest way is to use SDKMAN https://sdkman.io/:

  1. Install JDK 11 with:

    sdk install 11.0.11.hs-adpt
    
    • sdk list java for the list of available JDK
  2. Create a .sdkmanrc file in your project main directory:

    java=11.0.11.hs-adpt
    
  3. Switch JDK

    • use sdk env for switch to right JDK.

    • For automatic switch to the chosen jdk version, enable it in sdkman config file (~/.sdkman/etc/config)

      sdkman_auto_env=true
      
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Djacomo
  • 505
  • 4
  • 9
0

I was having the same error when packaging using Maven from VSCode. I just needed to add the release element to the mavin maven-compiler-plugin in the pom.xml like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
    </plugins>
</build>

Thank you to Bing Chat to nail that one!

Mark Johnson
  • 123
  • 2
  • 5
  • adding this results in a different failure, for me: Fatal error compiling... : invalid flag: --release – wkhatch May 09 '23 at 16:27
-2

First run

/usr/libexec/java_home -V

This will list all the possible JAVA_HOME paths possible on your machine.

You have two options:

Add the line (project permanent)

JAVA_HOME="/path/to/obtained/from/command/lib"

in your maven.bat file, ideally right after the initial comments.

Run

export JAVA_HOME=/path/to/obtained/from/command/lib

Before running the mvnw command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Kitonyo
  • 2,109
  • 1
  • 14
  • 17
  • On [Windows](https://en.wikipedia.org/wiki/Microsoft_Windows), presumably? What edition / version was this tested on? [Windows 10](https://en.wikipedia.org/wiki/Windows_10)? – Peter Mortensen Aug 20 '22 at 21:34
-3
  1. Check the Java home under path inside environment variables. It should point to JDK 11 under your Java installation directory,

  2. Check for the below inside file pom.xml

     <properties>
         <java.version>11</java.version>
     </properties>
    
  3. Under Path, check the correct entry for %JAVA_HOME% environment variable and explicitly set JAVA_HOME=C:\tools\java\jdk11

  4. Check, your mvn should not be overriding the Java version 11

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohit Singh
  • 5,977
  • 2
  • 24
  • 25
-6

This what helped me:

Changed from 11 to 8, works fine

The issue must be you are using 8 locally, but pointing to a different version in your pom.xml.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/68105347/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Aug 20 '22 at 21:36
-7

Just change this

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

to

<properties>
    <java.version>1.8</java.version>
</properties>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AzezoLinux
  • 35
  • 1