0

在 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.

google-java-formatter确实注意到--add-exports,在 JDK 16+ 上运行时,由于 JEP-396(JDK 内部的强封装),您需要在运行格式化程序时设置标志。不清楚的是如何--add-exports为 Eclipse 插件设置设置。

添加以下内容eclipse.ini(或在我的情况下SpringToolSuite4.ini)似乎没有帮助(更不用说感觉不对,因为它没有针对那个特定的插件)。有没有不同的方法来解决/解决这个问题?

-vmargs
-Dosgi.requiredJavaVersion=11
-Dosgi.dataAreaRequiresExplicitInit=true
-Xms256m
-Xmx2048m
--illegal-access=permit
--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
4

2 回答 2

1

对于仍然在 JDK 17 上运行此问题的任何人,只需在类似=之间添加--add-exports

--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
于 2022-01-04T12:30:12.463 回答
1

就像上面说的 xDeyan 一样,你的 eclipse.ini 文件必须在 -vmargs 行下面有以下几行

--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

这已经过测试并适用于 JDK17

于 2022-01-14T23:17:41.057 回答