JDK 17 Unable to make field int sun.util.calendar.BaseCalendar

2.3k Views Asked by At

This exception occurs after java migration to OpenJDK Runtime Environment Temurin-17.0.3+7 build 17.0.3+7

I have try to add add those JVM OPTIONS but not help

JVM_OPTIONS="$JVM_OPTIONS --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.base=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED --add-opens java.base/sun.util=ALL-UNNAMED"

Exception

2022-09-09 16:05:46,132 [ERROR] [pool-48-thread-5] : cannot close object
java.lang.reflect.InaccessibleObjectException: Unable to make field int sun.util.calendar.BaseCalendar$Date.cachedYear accessible: module java.base does not "opens sun.util.calendar" to unnamed module @35fc6dc4
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
    at com.rits.cloning.Cloner.addAll(Cloner.java:561)
    at com.rits.cloning.Cloner.allFields(Cloner.java:578)
    at com.rits.cloning.Cloner.cloneObject(Cloner.java:463)
    at com.rits.cloning.Cloner.cloneInternal(Cloner.java:454)
    at com.rits.cloning.Cloner.applyCloningStrategy(Cloner.java:488)
    at com.rits.cloning.Cloner.cloneObject(Cloner.java:471)
    at com.rits.cloning.Cloner.cloneInternal(Cloner.java:454)
    at com.rits.cloning.Cloner.applyCloningStrategy(Cloner.java:488)
    at com.rits.cloning.Cloner.cloneObject(Cloner.java:471)
    at com.rits.cloning.Cloner.cloneInternal(Cloner.java:454)
    at com.rits.cloning.Cloner$1.deepClone(Cloner.java:121)
    at com.rits.cloning.FastClonerArrayList.clone(FastClonerArrayList.java:19)
    at com.rits.cloning.Cloner.fastClone(Cloner.java:132)
    at com.rits.cloning.Cloner.cloneInternal(Cloner.java:439)
    at com.rits.cloning.Cloner.deepClone(Cloner.java:324)

any idea ?

1

There are 1 best solutions below

0
Artur Linhart On

We had this problem too, so I have also searched for the solution for this problem.

I would offer to you to use instead of Cloner the deep cloning through serialization into bytestream like described for example here in the chapter "Deep Copy using Serialization". It is true you will get larger garbage collection and need your objects to be throroughly serializable, but not only should it be safe, but you should be rewarded by better performance - I have performed cloning through serialization on some large objects list runned 1000 times by both methods over same data and my test gave following:

deep copy by serialization millis: 231

deep copy by Cloner millis: 324

Of course such test is not very over-representative, there is some difference in the case of the objects with more complex/less complex structure, but the difference +50% of processing time in the case of usage of Cloner gives, I think, good margin to be sure you will not not be worse with serialization cloning than with Cloner cloning.