Unable to compile simple Java 10 / Java 11 project with Maven

前端 未结 7 829
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 14:31

I have a trivial Maven project:

src
└── main
    └── java
        └── module-info.java
pom.xml

pom.xml:

org.         


        
7条回答
  •  渐次进展
    2020-11-22 14:35

    It might not exactly be the same error, but I had a similar one.

    Check Maven Java Version

    Since Maven is also runnig with Java, check first with which version your Maven is running on:

    mvn --version | grep -i java 
    

    It returns:

    Java version 1.8.0_151, vendor: Oracle Corporation, runtime: C:\tools\jdk\openjdk1.8

    Incompatible version

    Here above my maven is running with Java Version 1.8.0_151. So even if I specify maven to compile with Java 11:

    
        11
        ${java.version}
        ${java.version}
    
    

    It will logically print out this error:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project efa-example-commons-task: Fatal error compiling: invalid target release: 11 -> [Help 1]

    How to set specific java version to Maven

    The logical thing to do is to set a higher Java Version to Maven (e.g. Java version 11 instead 1.8).

    Maven make use of the environment variable JAVA_HOME to find the Java Version to run. So change this variable to the JDK you want to compile against (e.g. OpenJDK 11).

    Sanity check

    Then run again mvn --version to make sure the configuration has been taken care of:

    mvn --version | grep -i java
    

    yields

    Java version: 11.0.2, vendor: Oracle Corporation, runtime: C:\tools\jdk\openjdk11

    Which is much better and correct to compile code written with the Java 11 specifications.

提交回复
热议问题