InaccessibleObjectException keeps coming back after adding --add-opens
Your error
java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @5e4812481 more line it can showFix it fast
Most common cause, 1 of 4The flag is written after -jar app.jar
Move every JVM option, --add-opens included, in front of -jar.
java --add-opens java.base/java.lang=ALL-UNNAMED \
--add-opens java.base/java.util=ALL-UNNAMED \
-jar app.jarShort answer
When --add-opens does not stop InaccessibleObjectException, the flag never reached the failing JVM or opens a different package. Options after -jar app.jar go to main(), and Maven or Gradle fork separate JVMs for runs and tests. Put the flag before -jar, copy the package from the error, and set it in the forked JVM. Applies to JDK 17 to 27.
Which cause do you have?
While the app is running, ask the JVM which command line started it. The flag must appear there, spelled for the module and package named in quotes in your error. If it is missing, or it comes after the jar name, the JVM never treated it as an option.
jcmd -l
jcmd <pid> VM.command_lineThen find the line you see in this list and open its fix:
- 1The flag is written after -jar app.jar
The start command looks like java -jar app.jar --add-opens ..., or ./gradlew bootRun --args='--add-opens ...' - 2The opened package is not the one in the error
The error says does not "opens java.util" while your flag opens java.base/java.lang, or a new package fails each time you add one - 3Maven or Gradle runs the app or the tests in another JVM
java -jar works, but mvn spring-boot:run, ./gradlew bootRun or the test task still throws, or the other way round - 4The Add-Opens manifest entry is ignored
The jar manifest has an Add-Opens line, but the app is started with -cp or the line contains =ALL-UNNAMED
What does the error look like?
java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @5e481248module java.base does not "opens java.util" to unnamed module
Cause 1 of 4
The flag is written after -jar app.jar
You seeThe start command looks like java -jar app.jar --add-opens ..., or ./gradlew bootRun --args='--add-opens ...'
Check
InaccessibleObjectException survives an --add-opens flag placed after -jar app.jar because the java launcher passes everything after the jar file to the main method as program arguments. The JVM never sees it. Gradle's --args option for bootRun does the same: those values are application arguments, not JVM options.
grep -rn -- "-jar" Dockerfile docker-compose.yml *.service 2>/dev/nullFix
Move every JVM option, --add-opens included, in front of -jar. The format is module/package=target, with one flag per package.
java --add-opens java.base/java.lang=ALL-UNNAMED \
--add-opens java.base/java.util=ALL-UNNAMED \
-jar app.jarVerify
jcmd now shows both flags as JVM options and the exception is gone.
jcmd <pid> VM.command_lineConfirmed by docs.oracle.com
Cause 2 of 4
The opened package is not the one in the error
You seeThe error says does not "opens java.util" while your flag opens java.base/java.lang, or a new package fails each time you add one
Check
Each --add-opens flag opens exactly one package of one module, so the module and package must match the text in quotes in the exception. Opening java.lang does nothing for a reflective call into java.util. Libraries that use reflection often touch several packages, and the next one fails as soon as the first is open.
grep -o 'module [a-z.]* does not "opens [a-z.]*"' app.log | sort -uFix
Add one flag for every distinct module and package pair the log reports. JDK 17 made the old --illegal-access switch obsolete (the launcher ignores it with a warning), so no single setting opens everything.
# from: module java.base does not "opens java.util"
--add-opens java.base/java.util=ALL-UNNAMED
# from: module java.base does not "opens java.lang.reflect"
--add-opens java.base/java.lang.reflect=ALL-UNNAMEDVerify
The grep finds no new package names after a full run of the failing feature.
grep -c InaccessibleObjectException app.logConfirmed by openjdk.org
Cause 3 of 4
Maven or Gradle runs the app or the tests in another JVM
You seejava -jar works, but mvn spring-boot:run, ./gradlew bootRun or the test task still throws, or the other way round
Check
The Spring Boot Maven plugin runs the application in a forked process, so options given to mvn itself do not reach it. Surefire runs tests in a forked JVM too, and its argLine setting holds the options for that fork. In Gradle, bootRun is a JavaExec task and tests always run in separate JVMs; both take extra JVM options from jvmArgs.
grep -n "jvmArguments\|argLine" pom.xml
grep -n "jvmArgs" build.gradle build.gradle.kts 2>/dev/nullFix
Give the flag to the forked JVM in the build file. For a quick one-off, the java launcher also reads JDK_JAVA_OPTIONS from the environment and adds its content to the command line.
<!-- pom.xml: spring-boot-maven-plugin and maven-surefire-plugin -->
<configuration>
<jvmArguments>--add-opens java.base/java.lang=ALL-UNNAMED</jvmArguments>
</configuration>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
</configuration>
// build.gradle
tasks.named('bootRun') { jvmArgs('--add-opens', 'java.base/java.lang=ALL-UNNAMED') }
test { jvmArgs('--add-opens', 'java.base/java.lang=ALL-UNNAMED') }
# one-off, any launcher
export JDK_JAVA_OPTIONS="--add-opens java.base/java.lang=ALL-UNNAMED"Verify
The same goal or task that failed now passes, for example mvn test or ./gradlew test.
mvn -q testConfirmed by docs.spring.io
Cause 4 of 4
The Add-Opens manifest entry is ignored
You seeThe jar manifest has an Add-Opens line, but the app is started with -cp or the line contains =ALL-UNNAMED
Check
The JDK honours an Add-Opens attribute in the main manifest only when the jar is launched with java -jar. Its value is a space-separated list of module/package pairs with no target part; a pair whose module or package does not resolve is simply ignored.
unzip -p app.jar META-INF/MANIFEST.MF | grep -i add-opensFix
Write the pairs without =ALL-UNNAMED and start the app with java -jar. With Maven, add the attribute through the jar plugin's manifestEntries.
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Add-Opens>java.base/java.lang java.base/java.util</Add-Opens>
</manifestEntries>
</archive>
</configuration>
</plugin>Verify
The manifest line lists plain pairs, and java -jar app.jar runs without the exception and without extra flags.
java -jar app.jarConfirmed by docs.oracle.com
How do I stop it coming back?
- Keep JVM options in one place per launcher (the Dockerfile ENTRYPOINT, the systemd unit, the build plugin) and review them when you change the JDK.
- Log ManagementFactory.getRuntimeMXBean().getInputArguments() at startup so every environment shows the JVM options it really got.
- Track which library needs each --add-opens flag in a comment, and drop the flag once a release of that library stops using JDK internals.
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 on the same stack: QistLock, Dispatch Pro, Nuclear Home Video.