JDK 升級問題小結


JDK8 發布很久了,它提供了許多吸引人的新特性,能夠提高編程效率。

如果是新的項目,使用 JDK8 當然是最好的選擇。但是,對於一些老的項目,升級到 JDK8 則存在一些兼容性問題,是否升級需要酌情考慮。

近期,我在工作中遇到一個任務,將部門所有項目的 JDK 版本升級到 1.8 (老版本大多是 1.6)。在這個過程中,遇到一些問題點,並結合在網上看到的坑,在這里總結一下。

FAQ

sun.* 包缺失問題

JDK8 不再提供 sun.* 包供開發者使用,因為這些接口不是公共接口,不能保證在所有 Java 兼容的平台上工作。

使用了這些 API 的程序如果要升級到 JDK 1.8 需要尋求替代方案。

雖然,也可以自己導入包含 sun.* 接口 jar 包到 classpath 目錄,但這不是一個好的做法。

需要詳細了解為什么不要使用 sun.* ,可以參考官方文檔:Why Developers Should Not Write Programs That Call 'sun' Packages

默認安全策略修改

升級后估計有些小伙伴在使用不安全算法時可能會發生錯誤,so,支持不安全算法還是有必要的

找到$JAVA_HOME下 jre/lib/security/java.security ,將禁用的算法設置為空:jdk.certpath.disabledAlgorithms=

第三方jar包無法使用

有些第三方 jar 包基於非 JDK8 版本編譯,可能會存在兼容性問題。

這種情況只能具體問題具體分析,下面列舉幾個常用 jar 包。

  • 查找組件用到了 mvel,mvel 為了提高效率進行了字節碼優化,正好碰上 JDK8 死穴,所以需要升級。
<dependency>
  <groupId>org.mvel</groupId>
  <artifactId>mvel2</artifactId>
  <version>2.2.7.Final</version>
</dependency>
  • javassist
<dependency>
  <groupId>org.javassist</groupId>
  <artifactId>javassist</artifactId>
  <version>3.18.1-GA</version>
</dependency>

注意

有些部署工具不會刪除舊版本 jar 包,所以可以嘗試手動刪除老版本 jar 包。

JVM參數調整

在jdk8中,PermSize相關的參數已經不被使用:

-XX:MaxPermSize=size

Sets the maximum permanent generation space size (in bytes). This option was deprecated in JDK 8, and superseded by the -XX:MaxMetaspaceSize option.

-XX:PermSize=size

Sets the space (in bytes) allocated to the permanent generation that triggers a garbage collection if it is exceeded. This option was deprecated un JDK 8, and superseded by the -XX:MetaspaceSize option.

JDK8 中再也沒有 PermGen 了。其中的某些部分,如被 intern 的字符串,在 JDK7 中已經移到了普通堆里。其余結構在 JDK8 中會被移到稱作“Metaspace”的本機內存區中,該區域在默認情況下會自動生長,也會被垃圾回收。它有兩個標記:MetaspaceSize 和 MaxMetaspaceSize。

-XX:MetaspaceSize=size

Sets the size of the allocated class metadata space that will trigger a garbage collection the first time it is exceeded. This threshold for a garbage collection is increased or decreased depending on the amount of metadata used. The default size depends on the platform.

-XX:MaxMetaspaceSize=size

Sets the maximum amount of native memory that can be allocated for class metadata. By default, the size is not limited. The amount of metadata for an application depends on the application itself, other running applications, and the amount of memory available on the system.

以下示例顯示如何將類類元數據的上限設置為 256 MB:

XX:MaxMetaspaceSize=256m

資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM