Maven is not using Java 11: error message “Fatal error compiling: invalid target release: 11”

Maven is not using Java 11: error message “Fatal error compiling: invalid target release: 11”

To address the error message “Fatal error compiling: invalid target release: 11” when using Maven, follow these solutions to ensure your environment is correctly configured for Java 11.

Understanding the Error

This error typically arises when the Java version specified in your project does not match the version that Maven is using. It can occur if your JAVA_HOME environment variable points to an older JDK version or if the Maven configuration in your pom.xml is set incorrectly.

Solutions

Solution 1: Update JAVA_HOME

  1. Check JAVA_HOME: Ensure that your JAVA_HOME environment variable points to the correct JDK installation (Java 11).
   set JAVA_HOME=C:\path\to\jdk11

Place this command at the top of your mvn.bat file before calling Maven.

  1. Verify Installation: Run the following command to confirm that Java 11 is being used:
   java -version

Solution 2: Modify pom.xml Configuration

If your JAVA_HOME is set correctly but you still face issues, you may need to adjust your Maven configuration in the pom.xml file:

  1. Add Maven Compiler Plugin: Ensure you have the correct configuration for the Maven Compiler Plugin. Update or add the following section in your pom.xml:
   <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>
  1. Using Release Configuration: Alternatively, you can simplify this by using the release parameter:
   <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>3.8.0</version>
       <configuration>
           <release>11</release>
       </configuration>
   </plugin>

Additional Considerations

  • Check for Compatibility: Ensure that all dependencies and plugins used in your project are compatible with Java 11.
  • Update Dependencies: Regularly update your dependencies to their latest versions to avoid compatibility issues.

Conclusion

By ensuring that both your JAVA_HOME is pointing to JDK 11 and that your pom.xml is configured correctly, you should be able to resolve the “invalid target release: 11” error effectively. If issues persist, consider reviewing other configurations or dependencies that may be affecting your build process.

Citations:
[1] https://devwithus.com/fatal-error-compiling-invalid-target-release-11/
[2] https://github.com/swagger-api/swagger-codegen/issues/10421
[3] https://community.sonarsource.com/t/unable-to-resolve-dependency-when-scanning-in-java11-on-a-build-that-is-running-in-java8/31213
[4] https://winterbe.com/posts/2018/08/29/migrate-maven-projects-to-java-11-jigsaw/
[5] https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/maven-error-in-moving-code-from-java-8-to-11-in-aem-6-5/m-p/404177
[6] https://developercommunity.visualstudio.com/content/problem/1279954/maven-build-fail-fatal-error-compiling-invalid-tar.html
[7] https://www.spigotmc.org/threads/maven-java-8-java-11-compile-error.444933/
[8] https://github.com/pmd/pmd/issues/2484

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *