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

如何在Gradle 7.6-rc-1中启用Java 19预览/孵化特性?

Gradle配置Java 19预览特性与结构化并发

要将你提供的编译、运行脚本参数转换为Gradle配置,需使用Gradle 7.6及以上版本(包括7.6-rc-1),因为该版本开始支持Java 19。以下是完整的Groovy风格build.gradle配置示例:

基础配置(编译+运行)

plugins {
    id 'java'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(19)
    }
}

// 对应javac编译参数配置
tasks.withType(JavaCompile) {
    options.release = 19
    options.compilerArgs += [
        '--enable-preview',
        '--add-modules', 'jdk.incubator.concurrent'
    ]
}

// 对应java运行参数配置
tasks.named('run') {
    jvmArgs += [
        '--enable-preview',
        '--add-modules', 'jdk.incubator.concurrent'
    ]
    mainClass.set('net.codetojoy.Runner')
}

参数对应说明

  • options.release = 19:对应脚本中的--release 19,指定编译目标Java版本
  • --enable-preview:编译和运行时都需要添加,启用Java 19的预览特性(虚拟线程)
  • --add-modules jdk.incubator.concurrent:添加孵化模块以支持结构化并发特性
  • mainClass.set('net.codetojoy.Runner'):指定运行时的主类,对应脚本中的net.codetojoy.Runner

扩展:测试任务配置(如果需要)

如果你的测试代码也要用到预览或孵化特性,需额外配置测试任务:

tasks.named('test') {
    jvmArgs += [
        '--enable-preview',
        '--add-modules', 'jdk.incubator.concurrent'
    ]
}

tasks.withType(Test) {
    jvmArgs += [
        '--enable-preview',
        '--add-modules', 'jdk.incubator.concurrent'
    ]
}

内容的提问来源于stack exchange,提问作者Michael Easter

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 13:40:59