0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

jdk21環境でgoogle-java-formatを実行しようとしたらIllegalAccessError: class com.google.googlejavaformat.java.JavaInput...が出た話

Last updated at Posted at 2025-05-14

はじめに

JDK 21の環境でgoogle-java-formatをCLIから利用した際に遭遇したエラーと、その対応方法を紹介します。

発生したエラー

JDK 21環境でgoogle-java-format v1.14.0を使い、以下のようなコマンドを実行したところ、エラーが発生しました。

java -jar google-java-format-1.14.0-all-deps.jar --aosp --replace Sample.java

実際のエラーメッセージ

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

これはJDKのモジュールシステム(JEP 396による内部APIの非公開化)によって、google-java-formatがJDK内部のAPIにアクセスできなくなったために起こっていたようでした。

解決方法

google-java-format のリリースノートを確認したところ、v1.19.1 で JDK 21 に正式対応したことが明記されていました。今回のように、使用しているJDKのバージョンに合うgoogle-java-formatのバージョンを知りたい場合は、リリースノートにしっかり記載されているので、内容を追っていくことで確認できます。

v1.25.2でも問題なく動作することを確認済みです。

正しいCLIコマンド例(macOSの場合)

最新バージョンのjarファイルを使用すると問題が解消します。

java -jar google-java-format-1.25.2-all-deps.jar --aosp --replace Sample.java

Homebrewでインストールする場合

Homebrew経由でJDKとgoogle-java-formatをインストールすることもできます。

brew install openjdk google-java-format

この場合、以下のようにコマンドを実行します。

google-java-format --aosp --replace Sample.java

まとめ

  • JDK 21環境でgoogle-java-formatを使う場合、 v1.19.1以上が必須
  • CLIでの利用には追加の設定は不要

参考資料

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?