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

如何在IntelliJ IDEA社区版使用Java 19?模式匹配语法报错求助

解决IntelliJ IDEA不识别Java 19模式匹配解构语法的问题

IntelliJ IDEA是支持Java 19特性的,出现语法不识别的问题,大概率是配置没到位,按以下步骤排查解决:

  • 确认IntelliJ版本:必须使用2022.3及以上版本,旧版本对Java 19的支持不完善,直接升级到最新稳定版即可。

  • 配置项目Java SDK与语言级别:

    1. 打开File → Project Structure(快捷键Ctrl+Alt+Shift+S)
    2. 在Project Settings → Project中,将Project SDK设为Java 19,Project language level选择19 - Preview features(Java 19的switch模式匹配属于预览特性,必须开启该级别)
    3. 切换到Modules标签页,对应模块的Language level同样设为19 - Preview features
  • 开启编译器预览特性支持:

    1. 打开File → Settings → Build, Execution, Deployment → Compiler → Java Compiler
    2. 在Additional command line parameters中添加--enable-preview
    3. 如果是Maven项目,需要在pom.xml中补充编译插件配置:
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.11.0</version>
                  <configuration>
                      <source>19</source>
                      <target>19</target>
                      <compilerArgs>
                          <arg>--enable-preview</arg>
                      </compilerArgs>
                  </configuration>
              </plugin>
          </plugins>
      </build>
      
    4. Gradle项目则在build.gradle中添加:
      tasks.withType(JavaCompile) {
          options.compilerArgs += '--enable-preview'
          options.release = 19
      }
      
      test {
          jvmArgs += '--enable-preview'
      }
      
  • 清理缓存重启:执行File → Invalidate Caches... → Invalidate and Restart,解决缓存导致的识别异常。

如果以上操作后仍有问题,可以新建一个极简项目测试语法,排除项目本身配置冲突的可能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 04:15:18