google-java-format (and removeUnusedImports) broken on JDK 16+ (has workaround)

See original GitHub issue

When running under JDK 16 I get this error message:

Task :spotlessJava FAILED
Step 'google-java-format' found problem in 'src/main/java/se/inoviagroup/v2t/gateway/api/AuthUtils.java':
        ...
Caused by: java.lang.IllegalAccessError: class com.google.googlejavaformat.java.JavaInput (in unnamed module @0x615edab4) 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 @0x615edab4
        at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:349)
        at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:334)
        at com.google.googlejavaformat.java.JavaInput.<init>(JavaInput.java:276)
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:280)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:233)
        ... 142 more

EDIT:

EDIT 2 & 3

  • we thought no workaround necessary starting with plugin-gradle 6.5.1 and plugin-maven 2.22.3, but that was incorrect, the workaround is still required

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:29
  • Comments:28 (16 by maintainers)

github_iconTop GitHub Comments

52reactions
dymkcommented, Apr 13, 2021

@nedtwigg The following can be added to your gradle.properties to work-around the bug:

org.gradle.jvmargs=--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
8reactions
jmaichercommented, Apr 12, 2021

This can be done in maven via MAVEN_OPTS or ${maven.projectBasedir}/.mvn/jvm.config, see https://maven.apache.org/configure.html

Read more comments on GitHub >

github_iconTop Results From Across the Web

Eclipse Plugin --add-exports Java 16+ - Stack Overflow
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...
Read more >
diffplug/spotless - Gitter
rootDir) { include '**/*.java' exclude '**/generated/**' } googleJavaFormat() removeUnusedImports() } }. I'm not getting the same output from spotlessApply ...
Read more >
Google Java Formatter plugin broken after IDE update
This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong. User Agreement Review now...
Read more >
58978e6739ad1e1f1125b5d332d1136e9bb75b5d - platform ...
Fix handling of case patterns in unused import removal ... google-java-format is a program that reformats Java source code to comply with Google...
Read more >
com.google.googlejavaformat.java.RemoveUnusedImports ...
Formats an input string (a Java compilation unit) and fixes imports. * * <p>Fixing imports includes ordering, spacing, and removal of unused import ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found