目前,JDK兩個長期版本是8和11,因爲大部分項目使用的仍是8,因此從8升級到11會遇到一些困難。html
本篇文章會基於實踐遇到的問題,分類總結可能java
從Java 9開始,因爲引入了JPMS(https://openjdk.java.net/projects/jigsaw/spec/),開始更精細化的包管理和模塊複用,致使不少JDK默認加載的包被移除了。spring
其中咱們可能會碰到的例如:api
com.x.XMLUtils
:private static void getXmlFromObject(Book book) throws JAXBException { Marshaller marshallerObj = JAXBContext.newInstance(Book.class).createMarshaller(); marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); StringWriter sw = new StringWriter(); marshallerObj.marshal(book, sw); }
sun.misc.BASE64Encoder
,假設項目中位於com.x.EncodeUtil
:String encodedString = new BASE64Encoder().encode(inputString.getBytes());
經過JAVA 9 之後自帶的工具jdeps
,查看解決方法:閉包
jdeps target\APP.jar com.x.ReflectionUtils -> com.sun.crypto.provider JDK internal API (java.base) com.x.XMLUtils -> javax.xml.bind java.xml.bind com.x.EncodeUtil -> sun.misc JDK internal API (JDK removed internal API)
deps --jdk-internals target\pre-jpms.jar ... JDK Internal API Suggested Replacement ---------------- --------------------- sun.misc.BASE64Encoder Use java.util.Base64 @since 1.8 sun.reflect.Reflection Use java.lang.StackWalker @since 9
能夠看出,jdeps
這個工具,能夠找出過時的APi替換方法,也能夠找出缺失的模塊。oracle
對於JAXB,經過jdeps
命令咱們能夠知道須要添加java.xml.bind
這個模塊,經過添加java啓動參數--add-modules java.xml.bind
便可解決。maven
可是,這樣添加參數,不是長久的辦法,仍是添加Maven依賴,比較靠譜:ide
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency>
對於Spring Boot 2.x.x,能夠添加依賴:模塊化
<dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> </dependency>
對於Spring Cloud體系,須要的全部依賴是:工具
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>${jaxb.version}</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>${jaxb.version}</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>${jaxb.version}</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>${jaxb.version}</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>${activation.version}</version> </dependency>
報錯:maven編譯報錯:java.lang.ExceptionInInitializerError: com.sun.tools.javac.code.TypeTags
這是由於Java 9以後的字節碼編譯特性變化,須要升級到1.18.+的版本才能夠。
JDK 8 到JDK 11有不少參數變化,能夠總結爲兩類參數的變化,一是GC相關的(GC配置調優更加簡單),二是日誌相關的,日誌統一到了一塊兒,不像以前那麼混亂
具體請參考:
每一個說明參考三部分:
啓動的時候可能會報以下異常:
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/D:/Repositories/maven/org/springframework/spring-core/5.0.13.RELEASE/spring-core-5.0.13.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release
建議啓動的時候加上--illegal-access=warn
(其餘可選的還有"permit" "warn" "debug" "deny"),這裏建議是隻報警(WARN),以後留意升級這些相關的依賴,這樣避免之後JDK升級禁止這種反射,同時保證目前可用。