Eclipse Plugin --add-exports Java 16+

4.8k views Asked by At

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
3

There are 3 answers

1
xDeyan On

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
1
Jonathan Jogenfors On

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

0
Hachem saad On

Edit your file eclipse.ini with this arguments :

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