[Fixed] Under JDK 21, Maven compilation results in an error – Maven

by
Alexei Petrov
java-21 maven openjdk-21 spring-boot

Quick Fix: In your pom.xml file, specify the Java version to be used for compilation.
Add a properties tag and set the java.version property to 21.

Example:    <java.version>21</java.version>

The Problem:

Under JDK 21, Maven compilation fails with an error related to the usage of virtual threads when attempting to execute mvn install -DskipTests. The error specifically mentions methods like newVirtualThreadPerTaskExecutor() from java.util.concurrent.Executors. The project’s pom.xml includes an explicit configuration for the Maven compiler to use Java 21 as the source and target version, and the Java settings are properly configured in both ~/.m2 and the Maven installation’s conf directory. Relevant code and configuration details are provided for context.

The Solutions:

Solution 1: Add `java.version` property to the pom.xml file

Add the following property to your pom.xml file inside the `<properties>` tag:

&lt;properties&gt;
  &lt;java.version&gt;21&lt;/java.version&gt;
&lt;/properties&gt;

This tells Maven to use Java 21 for compiling and running your project. Make sure to replace `21` with the version of Java you want to use.

With this change, your pom.xml file should look something like this:

&lt;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 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;

  &lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;3.1.1&lt;/version&gt;
  &lt;/parent&gt;

  &lt;groupId&gt;com.example&lt;/groupId&gt;
  &lt;artifactId&gt;rms&lt;/artifactId&gt;
  &lt;version&gt;1.0&lt;/version&gt;
  &lt;packaging&gt;pom&lt;/packaging&gt;

  &lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;java.version&gt;21&lt;/java.version&gt;
    &lt;maven.compiler.source&gt;21&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;21&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;

  &lt;build&gt;
    &lt;pluginManagement&gt;
      &lt;plugins&gt;
        ...</plugins&gt;
      &lt;/plugins&gt;
    &lt;/pluginManagement&gt;
  &lt;/build&gt;
&lt;/project&gt;

After making this change, try compiling your project again. It should now compile successfully.

Q&A

Did you added this to your pom file?

Add this to your pom file: <java.version>21</java.version>

Did you configured both locations with the same settings?

Configure the settings with the same in both locations ~/.m2 and in the conf directory of Maven installation.

Have you tried setting the JAVA_HOME environment variable?

Set JAVA_HOME environment variable to the JDK 21 installation directory.

Video Explanation:

The following video, titled "”How", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

”…