Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 4

The flag is written after -jar app.jar

Move every JVM option, --add-opens included, in front of -jar.

Fix
java --add-opens java.base/java.lang=ALL-UNNAMED \
     --add-opens java.base/java.util=ALL-UNNAMED \
     -jar app.jar
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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.

  • Applies to JDK 25 LTS and JDK 27 (OpenJDK builds, including Amazon Corretto 25), JDK 17 and 21, Spring Boot 4.2.0 Maven and Gradle plugins, Gradle 9.9.2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
jcmd -l
jcmd <pid> VM.command_line

Then find the line you see in this list and open its fix:

  1. 1The flag is written after -jar app.jarThe start command looks like java -jar app.jar --add-opens ..., or ./gradlew bootRun --args='--add-opens ...'
  2. 2The opened package is not the one in the errorThe 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
  3. 3Maven or Gradle runs the app or the tests in another JVMjava -jar works, but mvn spring-boot:run, ./gradlew bootRun or the test task still throws, or the other way round
  4. 4The Add-Opens manifest entry is ignoredThe 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 @5e481248
  • module 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.

Check
grep -rn -- "-jar" Dockerfile docker-compose.yml *.service 2>/dev/null

Fix

Move every JVM option, --add-opens included, in front of -jar. The format is module/package=target, with one flag per package.

Fix
java --add-opens java.base/java.lang=ALL-UNNAMED \
     --add-opens java.base/java.util=ALL-UNNAMED \
     -jar app.jar

Verify

jcmd now shows both flags as JVM options and the exception is gone.

Verify
jcmd <pid> VM.command_line

Confirmed by docs.oracle.com

Did this fix it?

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.

Check
grep -o 'module [a-z.]* does not "opens [a-z.]*"' app.log | sort -u

Fix

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.

Fix
# 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-UNNAMED

Verify

The grep finds no new package names after a full run of the failing feature.

Verify
grep -c InaccessibleObjectException app.log

Confirmed by openjdk.org

Did this fix it?

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.

Check
grep -n "jvmArguments\|argLine" pom.xml
grep -n "jvmArgs" build.gradle build.gradle.kts 2>/dev/null

Fix

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.

Fix
<!-- 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.

Verify
mvn -q test

Confirmed by docs.spring.io

Did this fix it?

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.

Check
unzip -p app.jar META-INF/MANIFEST.MF | grep -i add-opens

Fix

Write the pairs without =ALL-UNNAMED and start the app with java -jar. With Maven, add the attribute through the jar plugin's manifestEntries.

Fix
<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.

Verify
java -jar app.jar

Confirmed by docs.oracle.com

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.