You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JDK 21环境下Maven编译报错:找不到newVirtualThreadPerTaskExecutor方法

解决Maven编译虚拟线程相关错误

问题详情

执行mvn install -DskipTests时,编译报错找不到Executors.newVirtualThreadPerTaskExecutor()方法,错误日志如下:

[ERROR] /E:/mycode/javaproject/rms/rms-parse/src/main/java/com/example/config/AsyncConfig.java:[19,49] 找不到符号
[ERROR]   符号:   方法 newVirtualThreadPerTaskExecutor()
[ERROR]   位置: 类 java.util.concurrent.Executors
[ERROR] /E:/mycode/javaproject/rms/rms-parse/src/main/java/com/example/config/AsyncConfig.java:[25,50] 找不到符号
[ERROR]   符号:   方法 newVirtualThreadPerTaskExecutor()
[ERROR]   位置: 类 java.util.concurrent.Executors
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[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/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :rms-parse

环境信息

  • Maven版本:3.9.5
  • JDK版本:21
  • 项目结构:
rms
  - rms-common
  - rms-parse
  - rms-admin
  - <some other sub-module>
  - pom.xml

关键配置代码

父模块pom.xml核心配置

<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>
<modules>
  <module>rms-parse</module>
  <module>rms-mapper</module>
  <module>rms-service</module>
  <module>rms-common</module>
  <module>rms-admin</module>
  <module>rms-model</module>
</modules>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
</properties>


<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>com.github.shalousun</groupId>
        <artifactId>smart-doc-maven-plugin</artifactId>
        <version>2.7.7</version>
        <configuration>
          <!--指定生成文档的使用的配置文件-->
          <configFile>${basedir}/src/main/resources/smart-doc.json</configFile>
          <!--指定项目名称-->
          <projectName>rms</projectName>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>html</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>21</source>
          <target>21</target>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

虚拟线程使用代码

@Configuration
@Slf4j
public class AsyncConfig {

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
    }

    @Bean
    public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
        return protocolHandler -> {
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };

    }
}

Maven settings.xml配置

<profiles>
  <profile>    
    <id>jdk-21</id>    
    <activation>    
      <activeByDefault>true</activeByDefault>    
      <jdk>21</jdk>    
    </activation>    
    <properties>    
      <maven.compiler.source>21</maven.compiler.source>    
      <maven.compiler.target>21</maven.compiler.target>    
      <maven.compiler.compilerVersion>21</maven.compiler.compilerVersion>    
    </properties>    
</profile>
</profiles>

问题原因

Executors.newVirtualThreadPerTaskExecutor()是JDK21中的预览API,默认编译时未启用预览特性,导致编译器无法识别该方法。同时Spring Boot 3.1.x版本对虚拟线程的支持需要配合开启预览参数才能正常使用。

解决方案

方案1:开启编译预览特性

修改父模块pom.xml中的maven-compiler-plugin配置,添加--enable-preview编译参数:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.11.0</version>
  <configuration>
    <source>21</source>
    <target>21</target>
    <!-- 添加预览特性开启参数 -->
    <compilerArgs>
      <arg>--enable-preview</arg>
    </compilerArgs>
  </configuration>
</plugin>

如果子模块有独立的pom.xml,需确保继承父模块的编译配置,或在子模块的maven-compiler-plugin中添加相同参数。

方案2:升级Spring Boot版本到3.2+

Spring Boot 3.2.x版本将虚拟线程转为正式支持特性,无需开启预览。修改父模块的Spring Boot版本:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.2.0</version> <!-- 或更高稳定版本 -->
</parent>

同时可以简化虚拟线程执行器的创建代码:

@Configuration
@Slf4j
public class AsyncConfig {

    @Bean
    public AsyncTaskExecutor asyncTaskExecutor() {
        return TaskExecutors.forVirtualThreads(); // Spring Boot提供的便捷方法
    }

    @Bean
    public TomcatProtocolHandlerCustomizer<?> protocolHandlerVirtualThreadExecutorCustomizer() {
        return protocolHandler -> {
            protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
        };
    }
}

额外验证步骤

  1. 执行mvn -v确认Maven使用的JDK版本为21,避免环境冲突。
  2. 执行mvn clean install -DskipTests清理并重新编译项目。

内容的提问来源于stack exchange,提问作者liang li

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:42:05