How to solve InaccessibleObjectException Unable to make member accessible module A does not 'opens package' to B on Java 9?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Understanding the InaccessibleObjectException
With the introduction of Java 9, the Java platform incorporated module functionalities, a fundamental change that aims to improve the structure and deployment of applications. This modular system provides better encapsulation and provides a more accessible API for developers. However, it also means that certain APIs that were previously accessible can now be restricted, leading to the InaccessibleObjectException. This particular exception occurs when your code attempts to access a method, field, or constructor that is not visible due to module encapsulation rules.
The full error message generally reads: "Unable to make {member} accessible: module {A} does not 'opens {package}' to {B}". Understanding and resolving this requires a blend of knowledge about Java modules along with access strategy manipulations.
Steps to Resolve InaccessibleObjectException
1. Understanding the Module System
Java 9 introduces a new module system. A module is a named, self-describing collection of code and data. Modules specify:
- Requires: Which other modules are dependencies.
- Exports: Which packages are accessible to other modules.
- Opens: Which packages are accessible at runtime via reflection.
2. Revisiting Your Module Declaration
To solve InaccessibleObjectException, you often need to modify your module descriptor (module-info.java) to explicitly allow access.
Example:
Suppose your module-info.java looks like this:
If you get InaccessibleObjectException, and the message indicates that a specific package needs to be opened, modify your module descriptor:
opens my.package to other.module: This line allows reflection on the specified package from the specified module.
3. Use of Command-Line Arguments
If modifying the module descriptor is not an option, or if encountering problems in a third-party library, using JVM command-line options can be a workaround solution.
You may employ options like --add-opens to dynamically open packages at runtime.
--add-opens: Allows you to specify which packages are open to other modules, dynamically.
Common Scenarios and Solutions
| Scenario | Solution |
| Accessing a third-party library | Use --add-opens option if you cannot alter the library's module descriptor. |
| Reflective access in multi-module projects | Modify the module descriptor using opens {package} to {module}. |
| Testing frameworks hitting this issue | Consider employing --add-opens globally, especially when dealing with testing tools that require reflective access into JDK modules. |
Advanced Techniques
Reflection with Care
When you have legitimate reasons to use reflection (like frameworks that heavily rely on it), be mindful of the modular system. Ensure that each reflection access is justified and handled properly within your module-info.java.
Utilizing Services
If you control both modules, assess if you can utilize Java's service mechanism. Implement a service provider interface (SPI) to decouple modules. It shifts the need from deep reflection to a service loader mechanism.
Conclusion
Dealing with InaccessibleObjectException involves an essential understanding of Java's module system and often requires strategic changes either in your module declarations or utilizing JVM arguments. The ultimate goal of these practices is better encapsulation and cleaner API resolutions. As you transition existing applications to Java 9 or later, familiarize yourself with these approaches to gracefully handle such exceptions. Balancing encapsulation through the module system with the demands of runtime reflection will ensure robust application behavior.
Related reading
- How to solve Plugin execution not covered by lifecycle configuration for Spring Data Maven Builds
- How to solve the Double-Checked Locking is Broken Declaration in Java?
- How to solve the “failed to lazily initialize a collection of role” Hibernate exception
- How to solve Timeout FeignClient
- How to solve nan loss?
- How to solve Permission denied publickey error when using Git?
- How to sort a List/ArrayList?
- How to sort a string list consists of digits and alphabets in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.