75

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?

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

12 Answers12

52

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 instead of IDE:

Linux:
echo $JAVA_HOME

Win:
echo %JAVA_HOME%

Then Build your project in the same terminal:

mvn clean package
msd.salehi
  • 839
  • 8
  • 14
30

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.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
17

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>
Lanil Marasinghe
  • 2,785
  • 24
  • 24
  • 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`. – BullyWiiPlaza May 28 '19 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.

Varesh
  • 1,648
  • 2
  • 14
  • 22
  • Adding `11` as property or and adding `11` as configuration to maven-compiler-plugin gives exactly the same result. Plugin documentation for [release](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#release) – Avec Nov 15 '19 at 06: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 Sep 05 '20 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. – Basil Bourque Apr 11 '21 at 20:52
10

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

<properties>
    <java.version>10</java.version>
</properties>
john peter
  • 101
  • 1
  • 2
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.

Nakesh
  • 546
  • 2
  • 7
  • 8
5

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.
Caffeine Coder
  • 948
  • 14
  • 17
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.

ItwasJJsmile
  • 111
  • 1
  • 5
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

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 07 '21 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.

Humphrey
  • 2,659
  • 3
  • 28
  • 38
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>
Sagar M
  • 1,168
  • 13
  • 10