4

Using the google-java-format eclipse plugin works great when running Eclipse under Java 11, but when running on Java 16+, it fails with the following error:

issue running code formatter

Full error: 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.

The google-java-formatter does note that when running on JDK 16+, you need to set the --add-exports flag when running the formatter, due to JEP-396 (strong encapsulation of JDK Internals). What isn't clear is how to tell set --add-exports for Eclipse plugins.

Adding the following to eclipse.ini (or in my case SpringToolSuite4.ini) doesn't seem to help (not to mention it feels wrong since its not targetting that particular plugin). Is there a different way to approach/fix this?

-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
sparty02
  • 566
  • 1
  • 6
  • 13
  • So why not just stick to Java 11 or the Java 15 that is bundled with most of the current Eclipse downloads? Eclipse doesn't have to be running Java 16 to develop Java 16 code. – greg-449 Sep 17 '21 at 20:17
  • Can you provide a few steps how to reproduce the issue, starting from a fresh STS 4.12.0.RELEASE install? I just tried that version, dropped the google javaformat plugin into it and switched to that implementation in the preferences, but I don't see the error appearing. – Martin Lippert Sep 20 '21 at 07:08
  • Without explicitly setting the -vm in eclipse.ini (or SpringToolSuite4.ini), Eclipse seems to use the platform default (that set via JAVA_HOME). In my case, I currently have that set to a JDK 17 instance, but I get the same experience on JDK 16. If you have google-java-formatter set as the formatter, and do a format (right click -> source format, or the keyboard shortcut) it doesn't show an error, but also doesn't actually format anything (it silently errors, though maybe its in the eclipse logs somewhere). In my case, I configured formatting as a Save Action, producing the error above – sparty02 Sep 20 '21 at 17:25

2 Answers2

11

Just like xDeyan said above, your eclipse.ini file must have the following lines below the -vmargs line

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

This has been tested and works with JDK17

2

For anyone still having this issue running on JDK 17, just add = between the --add-exports like so:

--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
xDeyan
  • 21
  • 3
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '22 at 13:20