Home >Java >javaTutorial >Java 9's InaccessibleObjectException: How to Resolve 'Unable to Make {Member} Accessible'?
Overcoming Java 9's InaccessibleObjectException: "Unable to Make {Member} Accessible"
Understanding the Issue
Java 9 introduces the Platform Module System, which enhances encapsulation by restricting access to certain elements. When an attempt is made to bypass these restrictions, an InaccessibleObjectException is thrown. The error message specifies the member being accessed, the module that restricts access, and the module attempting to access it.
Resolving the Exception
The solution varies depending on the scenario causing the issue.
1. Reflection Call Into JDK
Problem: A library or framework uses reflection to access elements within a JDK module.
Solution: Use command-line flags to open the specific package for reflection.
java --add-opens {jdk-module}/{package}={accessing-module}
For example:
java --add-opens java.base/java.lang=ALL-UNNAMED
2. Reflection Over Application Code
Problem: Reflection is being used to access elements within an application module.
Solution: Modify the application module's descriptor to open the package or module for access.
Options:
Export the package:
Export the package to a specific module:
Open the package:
Open the package to a specific module:
Open the entire module:
The appropriate choice depends on the level of access and visibility required.
The above is the detailed content of Java 9's InaccessibleObjectException: How to Resolve 'Unable to Make {Member} Accessible'?. For more information, please follow other related articles on the PHP Chinese website!