This article is based on OpenJDK11

During the RocketMQ upgrade from OpenJDK8 to OpenJDK11, the RocketMQ used by the system business MQ, the following exception was encountered when the JVM parameters were modified (refer to my other article) :

java.lang.IllegalStateException: java.lang.reflect.InaccessibleObjectException: Unable to make public void jdk.internal.ref.Cleaner.clean() accessible: module java.base does not "exports jdk.internal.ref" to unnamed module @3590fc5b
        at org.apache.rocketmq.store.MappedFileThe $1. The run (MappedFile. Java: 105) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at java.base/java.security.AccessController.doPrivileged(Native Method) ~[na:na] at Org. Apache. Rocketmq. Store. MappedFile. Invoke (MappedFile. Java: 98) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. MappedFile. Clean (MappedFile. Java: 94) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. MappedFile. Cleanup (MappedFile. Java: 434) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. ReferenceResource. Release (ReferenceResource. Java: 63) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. ReferenceResource. Shutdown (ReferenceResource. Java: 47) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. MappedFile. Destroy (MappedFile. Java: 442) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. Index. IndexFile. Destroy (IndexFile. Java: 89) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. Index. IndexService. Load (IndexService. Java: 71) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Store. DefaultMessageStore. Load (DefaultMessageStore. Java: 195) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0] the at Org. Apache. Rocketmq. Broker. BrokerController. The initialize (BrokerController. Java: 256) ~ [rocketmq - broker - 4.5.0. Jar: 4.5.0] the at org.apache.rocketmq.broker.BrokerStartup.createBrokerController(BrokerStartup.java:218) ~ [rocketmq - broker - 4.5.0. Jar: 4.5.0] at org. Apache. Rocketmq. Broker. BrokerStartup. Main (58) BrokerStartup. Java: ~ [rocketmq - broker - 4.5.0. Jar: 4.5.0] under Caused by: Java. Lang. Reflect. InaccessibleObjectException: Unable to make public void jdk.internal.ref.Cleaner.clean() accessible: module java.base does not"exports jdk.internal.ref" to unnamed module @3590fc5b
        at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:340) ~[na:na]
        at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280) ~[na:na]
        at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:198) ~[na:na]
        at java.base/java.lang.reflect.Method.setAccessible(Method.java:192) ~[na:na]
        at org.apache.rocketmq.store.MappedFileThe $1. The run (MappedFile. Java: 102) ~ [rocketmq - store - 4.5.0. Jar: 4.5.0]... 13 common frames omittedCopy the code

This is a drawback caused by modularity. We know that the concept of modularity, introduced after Java9, encapsulates types and resources in modules and exports only packages that other modules need to access their common types. If packages in a module are not exported or opened, the module’s designers have no intention of using them outside the module. Such packages may be modified or even removed from the module without notice. If you still use these packages by exporting or opening them using command-line options, you risk breaking your application!

So what do we do about this exception up here? This can be done by adding startup parameters.

First, such exceptions generally follow this template:

Unable to make {member} accessible: module {A} does not '{operation} {package}' to {B}Copy the code

According to operation, we decide to add the boot parameter:

operation JVM startup Parameters
exports –add-exports
opens –add-opens
requires –add-reads

Then concatenate the startup parameters:

{JVM startup parameters above} {A}/{package}={B}Copy the code

The parameter required for the above exception is all-unnamed which represents ALL anonymous classes compiled by the JDK:

--add-exports java.base/jdk.internal.ref=ALL-UNNAMEDCopy the code

See Java 9 Unwrapped (9. Breaking Module Encapsulation)