google / google-java-format

Reformats Java source code to comply with Google Java Style.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Eclipse plugin with Java 16+

sparty02 opened this issue · comments

The README has instructions for using the formatter with Java 16+ when running from the command line and instructions for using the Eclipse plugin. However, it's not clear how to use the Eclipse plugin with Java 16+ (which is the default JDK that ships with Eclipse at the moment...at least Spring Tool Suite 4.12).

Currently, when running Eclipse with Java 16+, this error pops up when attempting to format:

image

I'd be happy to PR a documentation update, but I'm honestly not sure how to define --add-exports for an Eclipse plugin.

Some things considered:

  • Does it go in eclipse.ini/SpringToolSuite4.ini under -vmargs, some context here?
  • If it goes in eclipse.ini, does that become effective for the entirety of eclipse? Would the google-java-formatter need to be modular in order to only apply it to that?

This does not seem connected to Eclipse but to Java 16+ in general. I get the following error when formatting my project with Gradle 7.2 / Java 17:

Caused by: java.lang.IllegalAccessError: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0xf69eaf7) 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 @0xf69eaf7

This does not seem connected to Eclipse but to Java 16+ in general

@christopher-cudennec see https://github.com/google/google-java-format#jdk-16

@cushon : Thanks for your link. I also add the Spotless issue for anyone who wants a workaround: diffplug/spotless#834

@sparty02 try adding the required exports in your 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

The first equals sign (after add-exports) is optional on the command line, but necessary (Oracle JDK 9 Migration Guide ) here because Eclipse uses the JNI Invocation API by default on most platforms.

With these changes the plugin works with Eclipse 4.22.0 and JDK 16/17.

Thanks @kukirsch that worked for me!