DEV Community

Cover image for [IntelliJ Fix] Fixing Spring Boot Startup Exception: java.lang.reflect.InaccessibleObjectException "opens java.lang"
Philip Mahama Akpanyi
Philip Mahama Akpanyi

Posted on

[IntelliJ Fix] Fixing Spring Boot Startup Exception: java.lang.reflect.InaccessibleObjectException "opens java.lang"

If you've encountered the java.lang.reflect.InaccessibleObjectException error while working on a Spring Boot project in IntelliJ, you're not alone! This issue often arises due to stricter module access restrictions in newer Java versions as explained here.

The Problem
When I created a Spring Boot project in IntelliJ, the application refused to start, throwing the following exception:

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field java.lang.ThreadLocal$ThreadLocalMap java.lang.Thread.threadLocals accessible: module java.base does not

This error is common when you're working with Java modules and missing the necessary --add-opens option to grant access.

The Solution
After some research, I discovered here that adding the JVM argument --add-opens java.base/java.lang=ALL-UNNAMED can resolve the issue. However, the tricky part was figuring out where to apply this fix in IntelliJ.

Here’s how I resolved it:

  • Open IntelliJ Run/Debug Configurations: Go to Run > Edit Configurations in the top menu.

Run/Debug Configurations

  • Locate Your Application: Select your Spring Boot application configuration. I use Wildfly.

  • Add JVM Options: In the "VM options" field, add: --add-opens java.base/java.lang=ALL-UNNAMED

  • Apply and Run: Save the changes, re-run your application, and voilà! No more startup exceptions.

Add --add-opens java.base/java.lang=ALL-UNNAMED

Key Takeaway
This fix works because it explicitly allows the JVM to open restricted modules to your application. Keep in mind that while this resolves the issue, it’s always good to stay informed about modularity and related best practices.

Have you encountered similar issues? Feel free to share your experience or any alternative solutions in the comments!

Top comments (0)