Java 9
InaccessibleObjectException
module system
troubleshooting
reflection

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.

Practice algorithms

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:

java
1module my.module {
2    requires some.dependency;
3    exports my.package; // allows compilation-time access
4}

If you get InaccessibleObjectException, and the message indicates that a specific package needs to be opened, modify your module descriptor:

java
1module my.module {
2    requires some.dependency;
3    exports my.package; 
4    opens my.package to other.module;
5}
  • 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.

bash
java --add-opens my.module/my.package=other.module -jar myapp.jar
  • --add-opens: Allows you to specify which packages are open to other modules, dynamically.

Common Scenarios and Solutions

ScenarioSolution
Accessing a third-party libraryUse --add-opens option if you cannot alter the library's module descriptor.
Reflective access in multi-module projectsModify the module descriptor using opens {package} to {module}.
Testing frameworks hitting this issueConsider 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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
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.

Practice algorithms