Compile error: package com.sun.tools.javac is not exported by module jdk.compiler
Module jdk.compiler does not export com.sun.tools.javac.utilShort answer
JDK 17 and newer no longer allow code to reach JDK internals such as com.sun.tools.javac by default. The usual culprit is an old annotation processor or build tool. Update it first, run Gradle with a JDK it supports, and only then open the package with --add-exports.
Fix it fast
1Find out which cause you have
Find out which JDK Gradle really runs on, and read the first line of the stack trace to see which tool touches javac.
./gradlew --version2Match what you see, then jump to its fix
- 1
The stack trace names a third-party class that calls com.sun.tools.javacAn old library, plugin or annotation processor uses JDK internalsFix - 2
The build worked before a JDK or Android Studio updateGradle runs on a newer JDK than the tool supportsFix - 3
Only one package is named in the error, and no newer tool existsYou cannot upgrade the tool, so the package must be openedFix
- 1
What does the error look like?
Module jdk.compiler does not export com.sun.tools.javac.utiljava.lang.reflect.InaccessibleObjectException
Cause 1 of 3
An old library, plugin or annotation processor uses JDK internals
1Check
Identify the processor in the trace and look for a newer release that supports your JDK.
./gradlew assembleDebug --stacktrace2Fix
Update that dependency or plugin to a version that does not rely on JDK internals.
# Raise the processor or plugin version in build.gradle(.kts) or the version catalog3Verify
Run the build again.
./gradlew clean assembleDebugConfirmed by docs.oracle.com
Cause 2 of 3
Gradle runs on a newer JDK than the tool supports
1Check
The JDK that runs Gradle in Android Studio is set in Gradle settings. In a terminal it is JAVA_HOME.
echo $JAVA_HOME
./gradlew --version2Fix
In Settings > Build, Execution, Deployment > Build Tools > Gradle, pick a Gradle JDK that your tools support and keep JAVA_HOME the same.
# Android Studio: Settings > Build Tools > Gradle > Gradle JDK3Verify
Sync Gradle and rebuild.
./gradlew --versionConfirmed by developer.android.com
Cause 3 of 3
You cannot upgrade the tool, so the package must be opened
1Check
Note the module and package in the message.
./gradlew assembleDebug --stacktrace2Fix
Grant access to that one package with --add-exports. It works until the internal API changes, so treat it as a stopgap.
# Option form from the JDK migration guide; pass it to the JVM or compiler that fails
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED3Verify
The error is gone and the build passes.
./gradlew assembleDebugConfirmed by docs.oracle.com
How do I stop it coming back?
- Pin the JDK with a Java toolchain so every machine compiles with the same one.
- Update annotation processors when you raise the JDK.
- Check the Gradle and JDK compatibility table before upgrading either.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
Android Studio or IntelliJ rejects the JDK chosen for Gradle
Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
The IDE could not use the JDK saved for this project's Gradle runs.
3 causes, each with a fixMobile apps
Android app crashes because the Apache HTTP classes are missing
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.
2 causes, each with a fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fix