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

Java 16+环境下Eclipse google-java-format插件报错如何配置--add-exports

Eclipse JDK16+环境google-java-format插件报错解决方案

问题描述

在Java 11环境下运行Eclipse时,google-java-format Eclipse插件功能正常,但在Java 16及以上版本运行时会触发非法访问错误:
运行代码格式化工具异常
完整报错信息如下:
A save participant caused problems. The save participant 'Code Clean Up' caused an exception: java.lang.IllegalAccessError: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0x99c5646) cannot access class com.sun.tools.javac.parser.Tokens$TokenKind (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.parser to unnamed module @0x99c5646. See the error log for details.
该问题是受JEP-396(JDK内部组件强封装)影响,JDK16开始默认不开放内部API给未命名模块,官方说明需要配置--add-exports参数,但很多用户将参数添加到eclipse.ini(或SpringToolSuite4对应的SpringToolSuite4.ini)后仍然不生效。

解决方案

方案1(优先推荐):切换Eclipse运行时JDK为11

Eclipse本身的运行JDK和业务项目的编译、运行JDK是完全独立的,你只需要将Eclipse/STS的运行环境切换为Java 11,业务项目的JDK版本仍然可以保留16+,完全不会影响项目开发。该方案不需要修改任何启动参数,从根源上避开了JDK强封装的限制,兼容性最好。

方案2:修正启动参数配置

如果必须使用JDK16+运行Eclipse本身,可以按以下步骤修正配置:

  1. 确认你修改的是当前使用的Eclipse/STS对应的ini文件,可右键启动快捷方式查看启动路径,确保ini文件和启动程序在同一目录
  2. 调整-vmargs后的参数顺序,删除已废弃的--illegal-access=permit参数,完整的正确配置参考如下:
-vmargs
-Dosgi.requiredJavaVersion=11
-Dosgi.dataAreaRequiresExplicitInit=true
-Xms256m
-Xmx2048m
--add-modules=ALL-SYSTEM
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
  1. 完全退出Eclipse/STS后重新启动,参数即可生效。

额外注意:请将google-java-format插件升级到最新稳定版,旧版本本身不兼容JDK16+运行环境,也会导致报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 03:51:02