阿东lazy

V2

2022/11/02阅读:381主题:橙心

How to Upgrade from Java 8 to Java 17

Upgrade steps

Introduction

Although I don't know anything about grammar, I still record what I read in English blog posts in English......

Source:https://medium.com/javarevisited/how-to-upgrade-from-java-8-to-java-17-eb58f4554c6e Here are some bits and pieces of notes.

1. Upgrade the Maven compiler plugin version

the example maven version is at least 3.5.4。

<plugin>

	<groupId>org.apache.maven.plugins</groupId>
	
		<artifactId>maven-compiler-plugin</artifactId>
		
		<version>3.8.1</version>
		
		<configuration>
		
		<source>17</source>
		
		<target>17</target>
	
	</configuration>

</plugin>

2. Upgrade dependency

the dependency you used originally probably doesn’t support Java 17 at all if you want to minimize the impact of version upgrade, upgrade your dependency

must be upgrade your dependency.

For example:

Lombok

  1. Lombok causes compilation errors. The error information is as follows
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project encloud-common: Fatal error compiling: java.lang.ExceptionInInitializerError: Unable to make field private com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs accessible: module jdk.compiler does not "opens com.sun.tools.javac.processing" to unnamed module

Work around:upgrading Lombok to the latest version can solve the problem.

Why? Old Lombok version uses the classes under the com.sun.tools.javac.*package

Zookeeper

An UnresolvedAddressException exception may be thrown,and the error information is as flows:

org.apache.zookeeper.ClientCnxn - Session 0x0 for server 10.0.*.*/<unresolved>:2181, unexpected error, closing socket connection and attempting reconnect java.nio.channels.UnresolvedAddressException: null at sun.nio.ch.Net.checkAddress(Net.java:149) ~[?:?] at sun.nio.ch.Net.checkAddress(Net.java:157) ~[?:?] at sun.nio.ch.SocketChannelImpl.checkRemote(SocketChannelImpl.java:816) ~[?:?] at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:839) ~[?:?] at org.apache.zookeeper.ClientCnxnSocketNIO.registerAndConnect(ClientCnxnSocketNIO.java:277) ~[zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03] at org.apache.zookeeper.ClientCnxnSocketNIO.connect(ClientCnxnSocketNIO.java:287) ~[zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03] at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1021) ~[zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03] at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1064) [zookeeper-3.4.13.jar:3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03]

The reason for the error is that java15 has refactored(重构) the toString method of InetSocketAddressHolder.

The solve is you can use the mvn versions:display-dependency-updates command to check for dependency updates

JAXB

JAXB is removed from Java 11,If you compile with JAXB, you will report an error.

The solution was to use Maven external dependencies to bring in, but I think this stuff is too old.

What is JAXB Java Architecture for XML Binding (JAXB) is an industry standard and is a technology for generating Java classes from XML Schema. In this process, JAXB also provides a method to reverse generate the Java object tree from the XML instance document, and can rewrite the contents of the Java object tree to the XML instance document. Java  Architecture for XML Binding (JAXB) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例文档。

Handle InaccessibleObjectException

Reflections are often used in projects, But in Java 17. If you use the native reflection method, especially setAccessible(true), you are likely to throw an InaccessibleObjectException when running your application.

The exception is caused by the Java platform module system, Java platform module system two conditions to be met for reflection to be used:

1. The class must be public. 2. You must export your own packages.

The solution was to use a rude way to force a module to be opened to the public. add the --add-opens parameter to the java startup command line

# --add-opens has the following syntax: {modules}/{package}=ALL-UNNAMED  
java --add-opens java.base/java.lang=ALL-UNNAMED

Or you can modify your Java code

module java.base {
 
 exports java.lang;

}

3. Responses

Here are some comments and feedback:

  1. From my experience, the biggest issue is not the code itself, but the surrounding infrastructure(周边设施). For example replacing Jenkins plugins that don't support Java 11 was painful.
  2. The biggest issue in my experience migrating java apps from version 8 it's related with java time precission:

# Increase the precision of the implementation of java.time.Clock.systemUTC()

LocalDateTime to milliseconds difference in Java 8 and Java 11 - Stack Overflow

Sometimes there is a reason not to upgrade the JDK,Changes to the internal logic of the time function are fatal to most systems

End

The first English blog note, commemorate it, click.

be interested in

  1. But so far, although java17 has been released for some time, java8 released eight years ago is still the most widely used java version.
  2. Java 8 has long stopped free maintenance.
  3. performance of Java 17 has been greatly improved
  4. Handle InaccessibleObjectException
  5. From my experience, the biggest issue is not the code itself, but the surrounding infrastructure(周边设施)
  6. LocalDateTime to milliseconds difference in Java 8 and Java 11 - Stack Overflow

分类:

后端

标签:

Java

作者介绍

阿东lazy
V2

摸鱼JAVA工程师 公众号:懒时小窝