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

如何在Visual Studio Code中启用预览功能解决Java 16 sealed class报错?

VS Code启用Java 16预览功能解决sealed class报错方案

你遇到的报错信息如下:

Sealed Type is a preview feature and disabled by default. Use --enable-preview to enable

可按以下步骤操作解决:

1. 确认基础JDK配置

  • 确保你已安装Java 16及以上版本,且VS Code已安装「Extension Pack for Java」扩展
  • 按下Ctrl+Shift+P(Mac系统为Cmd+Shift+P)唤起命令面板,输入Java: Configure Java Runtime,确认当前项目绑定的JDK版本为Java 16+

2. 按项目类型添加预览功能参数

场景A:无构建工具的普通Java项目

  • 按下Ctrl+,打开VS Code设置,搜索java.configuration.runtimes,点击「在settings.json中编辑」
  • 在配置中新增/修改Java 16的运行时参数,示例如下:
"java.configuration.runtimes": [
  {
    "name": "JavaSE-16",
    "path": "你的JDK 16本地安装路径",
    "default": true,
    "vmargs": "--enable-preview"
  }
]
  • 继续在settings.json中添加编译参数:
"java.compiler.args": [
  "--release", "16",
  "--enable-preview"
]

场景B:Maven项目

  • 打开项目根目录的pom.xml,修改maven-compiler-plugin配置:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.8.1</version>
  <configuration>
    <release>16</release>
    <compilerArgs>
      <arg>--enable-preview</arg>
    </compilerArgs>
  </configuration>
</plugin>

场景C:Gradle项目

  • 打开项目根目录的build.gradle,添加如下配置:
tasks.withType(JavaCompile) {
    options.release = 16
    options.compilerArgs += "--enable-preview"
}
tasks.withType(Test) {
    jvmArgs += "--enable-preview"
}
tasks.withType(JavaExec) {
    jvmArgs += "--enable-preview"
}

3. 重启生效

  • 按下Ctrl+Shift+P唤起命令面板,输入Java: Clean Java Language Server Workspace,执行后根据提示重启VS Code即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 08:48:02