Home > Java > javaTutorial > Java 9's InaccessibleObjectException: How to Resolve 'Unable to Make {Member} Accessible'?

Java 9's InaccessibleObjectException: How to Resolve 'Unable to Make {Member} Accessible'?

DDD
Release: 2025-01-03 07:58:39
Original
809 people have browsed it

Java 9's InaccessibleObjectException: How to Resolve

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}
Copy after login

For example:

java --add-opens java.base/java.lang=ALL-UNNAMED
Copy after login

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:

    • exports {package}
  • Export the package to a specific module:

    • exports {package} to {accessing-module}
  • Open the package:

    • opens {package}
  • Open the package to a specific module:

    • opens {package} to {accessing-module}
  • Open the entire module:

    • open module {module-to-open} {...}

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template