1

Preface

Recently, I was making technical selections for the company’s new architecture. It just happened that Java 17 was officially released for a while, and it was a LTS . Just think about using it directly. Some of the features are still very easy to use, such as:

Problems encountered

The main reason is that after Java modularization, some jdk internal classes cannot be accessed, but before Java 16 they are only warnings, and after Java 16 they will report errors directly. Currently, frameworks that cglib and javassist As a result, the project cannot be started, and the following exception is thrown:

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @39aeed2f
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
    at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
    at net.sf.cglib.core.ReflectUtils$1.run(ReflectUtils.java:61)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
    at net.sf.cglib.core.ReflectUtils.<clinit>(ReflectUtils.java:52)
    at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:243)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:332)

Starting from Java 16, JEP 396 will set the --illegal-access parameter to deny by default, which means that access to packaged packages and reflection of other modules are disabled by default, which will cause the above exception. The default value of this parameter has always been --illegal-access=permit . Only warnings will be generated, but no errors will be reported, so if it is Java 16, you need to set permit --illegal-access when executing the Java program, so that the problem can be solved, example:

java -jar --illegal-access=permit app.jar

Starting from Java 17, it is . 1619c917ea6680 JEP 403 directly removes the --illegal-access parameter. If you need to enable access to the packaged package, you need to add the --add-opens java.base/java.lang=ALL-UNNAMED option when executing the Java program. Example:

java -jar --add-opens java.base/java.lang=ALL-UNNAMED app.jar

If you are running in IDEA, you need to configure the corresponding VM parameters, for example:

Although it is said that you can run after adding parameters, I think this is a destructive change, because in this case, if one day the Java version changes and the parameters become invalid, then all items need to be updated, which will lead to The maintenance cost of the project has greatly increased, so it is not recommended here.

Open source framework upgrade progress tracking

So is there a way to run normally without adding startup parameters? The answer is yes, but I need to wait for the open source framework to fully convert the new API of Java 17. At present, the two projects I tracked have not yet adapted to Java 17.

Spring

SpringBoot 2.5.0 started to support Java 17, no problem.

apollo configuration center

apollo current master branch code is already adapting well, but has not yet issued a formal version, rather strange thing is, apollo before upgrade the underlying dependencies to adapt Java 17, but later rollback back , I don’t know why.

dubbo

Dubbo has a issue 7593 which was raised in April, but no one has followed up.

Summarize

After a meal, I found that it didn't work, so I changed it to Java 15 first, and then upgraded to Java 17 when the time was right.

I am MonkeyWie , welcome to scan the QR code 👇👇 to follow! From time to time, share the dry goods knowledge such as JAVA , Golang , front end, docker , k8s

wechat


mokeyWie
2.5k 声望642 粉丝

全干工程师~