0

My application was developed in OracleJDK8 and recently i upgraded to OpenJDK11. After upgraded to OpenJDK11 , I have faced

java.lang.reflect.InaccessibleObjectException: Unable to make public long com.sun.management.internal.OperatingSystemImpl.getCommittedVirtualMemorySize() accessible: module jdk.management does not "opens com.sun.management.internal" to unnamed module @59aae9d8
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:340)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:280)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:198)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:192)
    at org.stack.oasis.controller.ApplicationController.systemHealth(ApplicationController.java:912)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)

Please provide alternative way to find a system memory.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Prakash Nagaraj
  • 533
  • 4
  • 12
  • 3
    Welcome to the world of modules. Don’t use internal APIs – Thorbjørn Ravn Andersen Oct 21 '19 at 18:46
  • 2
    You can adapt the solution of [this answer](https://stackoverflow.com/a/48326083/2711488) to query the `"CommittedVirtualMemorySize"` attribute in a way independent of internal APIs and not needing access override. Generally, your code has fallen for an old Reflection problem. Calling `someObject.getClass().getMethod(...)` may result in an inaccessible `Method` that is actually implementing an accessible (e.g. `interface`) method. Looking up the interface method instead, would lead to a `Method` that could be called on the implementation instance without problems, even without `setAccessible`. – Holger Oct 22 '19 at 08:01

0 Answers0