79

I'm trying to compile my maven project using Java 10 but I'm having trouble. In my IDE (IntelliJ IDEA) everything compiles and runs just fine under Java 10. I installed the latest maven version 3.5.4 and pointed my JAVA_HOME to the JDK 10:

$ mvn --version
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T20:33:14+02:00)
Maven home: C:\Maven\bin\..
Java version: 10.0.2, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk-10.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

The maven command I use is:

mvn package -Dmaven.test.skip

In my pom.xml I had the following for Java 8 which worked fine:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <libraries.directory>libraries</libraries.directory>
</properties>

For Java 10 I changed it to the following:

<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>

This does not work, just like using 1.10 as the version for the source and target compiler.

The full debug logging output (-X command line switch) is the following:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project My-Project: Fatal error compiling: invalid target release: 1.10 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project My-Project: Fatal error compiling
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Fatal error compiling
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1086)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:168)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.codehaus.plexus.compiler.CompilerException: invalid target release: 1.10
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess (JavaxToolsCompiler.java:173)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile (JavacCompiler.java:174)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1075)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:168)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.lang.IllegalArgumentException: invalid target release: 1.10
    at com.sun.tools.javac.main.OptionHelper$GrumpyHelper.error (OptionHelper.java:103)
    at com.sun.tools.javac.main.Option$12.process (Option.java:216)
    at com.sun.tools.javac.api.JavacTool.processOptions (JavacTool.java:217)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:156)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:107)
    at com.sun.tools.javac.api.JavacTool.getTask (JavacTool.java:64)
    at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess (JavaxToolsCompiler.java:125)
    at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile (JavacCompiler.java:174)
    at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute (AbstractCompilerMojo.java:1075)
    at org.apache.maven.plugin.compiler.CompilerMojo.execute (CompilerMojo.java:168)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:954)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR]
[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

Adding the maven-compiler-plugin like suggested in this answer also does not work:

Fatal error compiling: invalid flag: --release -> [Help 1]

Any ideas on how to fix this problem?

5
  • 2
    This was fixed in maven-compiler-plugin version 3.7.1. See issues.apache.org/jira/browse/MCOMPILER-332
    – rodrigoap
    Commented Jul 22, 2018 at 20:42
  • @rodrigoap Interesting but maven central does not have version 3.7.1 yet so it isn't found: mvnrepository.com/artifact/org.apache.maven.plugins/… Commented Jul 22, 2018 at 20:50
  • When you run with -X do you see Using compiler 'javac'. and what does it show for Command line options:? Commented Jul 22, 2018 at 21:38
  • 2
    The log reads failure when the incorrect version is specified. Make sure <release>10<release> instead of 1.10. Share the logs with 10, to see what the actual failure is.
    – Naman
    Commented Jul 23, 2018 at 3:52
  • @BullyWiiPlaza mvnrepository is not Central. This can be found here: search.maven.org
    – khmarbaise
    Commented Jul 23, 2018 at 8:22

12 Answers 12

53

Same problem fixed for me by setting JAVA_HOME environment variable to point to the JDK with right version.

My source and destination java version in maven-compiler-plugin were 11, java -version was also on version 11, but my JAVA_HOME was pointing to JDK-1.8.

Check JAVA_HOME again by using a terminal/cmd:

Linux:
echo $JAVA_HOME

Win:
echo %JAVA_HOME%

Then change it if it is incorrect (the following commands only apply to the same terminal/cmd session and will not make permanent change to the JAVA_HOME environment variable):

Linux:
export JAVA_HOME="CORRECT_PATH"

Win:
SET JAVA_HOME="CORRECT_PATH"

Then Build your project in the same terminal to check:

mvn clean package

If successful, make the permanent change to the JAVA_HOME environment variable.

1
  • 1
    This worked for me also. Go to Environment Variable on Windows then System Variables and edit the PATH variable with the Java JDK Path Commented Jun 15, 2021 at 19:41
31

This worked for me:

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

This answer says you need:

<dependencies>
    <dependency>
        <groupId>org.ow2.asm</groupId>
        <artifactId>asm</artifactId>
        <version>6.2</version> <!-- Use newer version of ASM -->
    </dependency>
</dependencies>

inside the <plugin/> tag, but the newer mvn (3.5.4) must obviate that, since I didn't need it.

9
  • 1
    Regardless of the dependency added or not, I still get Fatal error compiling: invalid flag: --release -> [Help 1]. javac -help says --release <release> Compile for a specific VM version. Supported targets: 10, 6, 7, 8, 9. Commented Jul 22, 2018 at 21:04
  • 1
    The simple thing is you should use <maven.compiler.release>9</maven.compiler.release> instead of <maven.compiler.source>..</maven.compiler.sources> and <maven.compiler.target>..</maven.compiler.target> starting with JDK 9 cause JDK 8 does has a --release option...
    – khmarbaise
    Commented Jul 23, 2018 at 8:30
  • 1
    maven-compiler-plugin=3.7.0 + release=10 worked like a charm. Commented Oct 6, 2018 at 23:26
  • 2
    Changing JAVA_HOME may also be necessary: stackoverflow.com/a/52139841/2568885 Commented Oct 7, 2018 at 21:10
  • 3
    for java version from 1.6 to 1.10: <java.version>1.x</java.version> is fine. But for 11, it should be "11" rather than "1.11".
    – karl li
    Commented Feb 22, 2019 at 3:09
18

Get your java version by running java -version.

You'll get a output like :

java version "1.8.0_201"

Now go to your POM file and update source and target properities of maven-compiler-plugin with your installed java version. In my case it's 1.8

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
      <source>1.8</source>
      <target>1.8</target>
   </configuration>
</plugin>
1
  • 7
    Good hint about checking the installed Java version but the question was rather about using Java 10 and not reverting it back to 8. Commented May 28, 2019 at 20:52
14

I had a similar problem with spring boot and java 11. I solved it by defining the following property in my pom

<maven.compiler.release>11</maven.compiler.release>

Might be useful for someone in the future.

3
  • Adding <maven.compiler.release>11</maven.compiler.release> as property or and adding <release>11</release> as configuration to maven-compiler-plugin gives exactly the same result. Plugin documentation for release
    – Avec
    Commented Nov 15, 2019 at 6:51
  • Also make sure using mvn --version that you run at least Maven: 3.6.0 which uses underneath at least Java 9. There is no "--release" option for older versions.
    – luke
    Commented Sep 5, 2020 at 10:38
  • This tag maven.compiler.release replaces the maven.compiler.source & maven.compiler.target tags. Comment-out the source & target lines if present when using the release tag. Commented Apr 11, 2021 at 20:52
10

This means you are using java.version 10 which you have not installed.

<properties>
    <java.version>10</java.version>
</properties>
6

As you are using Intellij, you need to follow below steps (Here I'm using Java 11, but the same applies to Java 10 and other versions as well)-

  1. Change SDK under Project Structure to Java 11

enter image description here

enter image description here

  1. Change language level for modules (if needed) under Project Structure

enter image description here

  1. Change JRE under runner in settings

enter image description here

  1. Change Target bytecode version under Java compiler in Settings

enter image description here

  1. Check maven version under Maven in settings for supporting Java 11 enter image description here

  2. In your pom, mention the version as follows, you can ignore the encoding tag here

enter image description here

  1. Lastly confirm your run configuration before building project

enter image description here

  1. If you are using Windows machine, then change environment variables JAVA_HOME, Path and MAVEN_HOME accordingly.
6

Check your JAVA_HOME version. It seems your java version in JAVA_HOME and in POM is different. Set JAVA_HOME path properly by editing in Environment variable in Windows.

1

Sometimes it's just your java version. For example, when the project uses Java 10 but your Java Path is set to 8, etc. Try running java -version and make sure it's the same as your project. If not, fix it and try again.

1

thanks to the answer above, You are using 10, i use 11, but i guess the steps are similar.

the whole point for me is to explicitly specify the version as java 11 in my pom.xml, and I solved the problem by taking the following steps.

  1. open terminal and input: java -version, then i got 1.8, and i went to .bash_profile to switch the java version to 11.2 as i have multiple java version installed. please remember "restart" your terminal and check your java version again to confirm switch is successful.

  2. Then i went to File -> Project Structure to make sure my IntelliJ using the same version as my env, which is 11.2.

  3. RESTART your Intellij, mvn clean install, solved, hope this could help someone with this issue, thanks.

0

In "File >> Project Structure >> Project Settings >> Project >> Project SDK" choose the version of your Java installed and click OK.

It solved my Maven problem:

Invalid Target Release: 16 .

Image

1
  • 1
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
    – Community Bot
    Commented Sep 7, 2021 at 21:23
0

In my case I just went to terminal then I typed java -version then pressed enter. That gave me my java version which was 1.8 then I went to right click on my project folder then I clicked open POM. Then in the pom file there was this code

<properties>
        <java.version>17</java.version>
    </properties>

I just changed the 17 to 1.8 like this

<properties>
        <java.version>1.8</java.version>
    </properties>

I saved and I closed the IDE opened again run my program then all was fine I hope that helps.

0

Solution : add this line in pom.xml

You need to add your own context in pom.xml.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.