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:
<properties>
<java.version>21</java.version>
</properties>
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:
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
</parent>
<groupId>com.example</groupId>
<artifactId>rms</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
...</plugins>
</plugins>
</pluginManagement>
</build>
</project>
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:
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.
”…
The following video, titled "”How", provides additional insights and in-depth exploration related to the topics discussed in this post.
”…