[JDK 11] [JDK 8] Project jdk version upgrade, modification plan and practice

foreword

At work, it is inevitable that you will encounter upgrades. This time, because of the two systems, system A uses JDK8 version, and system B uses JDK11 version; it is required to synchronize the JDK version, that is, to upgrade to version 11. Then I will introduce it next~

problem and solution

1. The problem that the .sh script cannot be started

problem dicovered

Since there is no change in the script, there is no problem in starting jdk8 and it can be started successfully, but jdk11 cannot be started.

cause

(1) JDK 11 already 弃用has -XX:PermSizeparameters and -XX:MaxPermSizeparameters, which will cause startup failure. (These two parameters are used to set the initial size and maximum size of permanent generation memory in JDK 8 and earlier versions.)
(2) -XX:+PrintGCDateStampsThis parameter is supported in both JDK 8 and JDK 9, but may be used in previous versions There will be cases where it is not supported.

The solution

(1) ① Remove -XX:PermSizethe parameters and -XX:MaxPermSizeparameters. ② Use parameter and instead of parameter and parameter on
JDK 11. (Note: The parameter and parameter in JDK 11 are to set the size of the metaspace memory.) (2) Remove this parameter.-XX:MetaspaceSize-XX:MaxMetaspaceSize-XX:PermSize-XX:MaxPermSize
-XX:MetaspaceSize-XX:MaxMetaspaceSize-XX:+PrintGCDateStamps

2. javafx.util.Pair replacement

problem dicovered

Appears when starting the project Error:(27, 19) java: 程序包javafx.util.Pair不存在.

cause

JDK 11It is no longer embedded javafx.

The solution

(1) If it is only used javafx.util.Pair, it can be replaced by other imported dependencies. (such cn.hutool.core.lang.Pairas etc.)
(2) The project introduces related dependencies of JavaFX.

[Introduce JavaFX related dependencies] (click to expand)
<dependencies>
   <dependency>
       <groupId>org.openjfx</groupId>
       <artifactId>javafx-controls</artifactId>
       <version>11</version>
   </dependency>
   <dependency>
       <groupId>org.openjfx</groupId>
       <artifactId>javafx-fxml</artifactId>
       <version>11</version>
   </dependency>
</dependencies>

3. sun.misc.BASE64Decoder 替换

发现的问题

启动项目的时候,出现 Error:(5, 16) java: 程序包sun.misc不存在

导致的原因

JDK 9 之后已经删除和弃用 sun.misc

解决的办法

(1) 如果是需要替换sun.misc.BASE64Decoder,可以考虑 java.util.Base64代替掉 sun.misc.BASE64Encodersun.misc.BASE64Decoder

【sun.misc.BASE64Decoder 替换 java.util.Base64 实例】(点击展开)
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import java.util.Base64;

/**
 * TODO:升级 jdk11 替换 被弃用 base64 示例
 *
 * @Author: nanfangzhe
 * @DateTime: 2023/6/1 20:10
 **/

public class Base64Utils {
    public static void main(String[] args) {
        String txt = "南方者";
        try {
            String oldEncode = OldBase64Decode.base64Encode(txt.getBytes());
            System.out.println("sun.misc.BASE64Decoder Encode:" + oldEncode);
            String newEncode = NewBase64Decode.base64Encode(txt.getBytes());
            System.out.println("java.util.Base64 Encode:" + newEncode);

            byte[] oldBytes = OldBase64Decode.base64Decode(oldEncode);
            System.out.println("sun.misc.BASE64Decoder Decode:" + new String(oldBytes));
            byte[] newBytes = NewBase64Decode.base64Decode(newEncode);
            System.out.println("java.util.Base64 Decode:" + new String(newBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

// jdk 8之前的版本
class OldBase64Decode {

    /**
     * TODO:Decode
     */
    public static byte[] base64Decode(String txt) throws Exception {
        if (StringUtils.isBlank(txt)) {
            return null;
        }
        byte[] bytes = new BASE64Decoder().decodeBuffer(txt);
        return bytes;
    }

    /**
     * TODO:Encode
     */
    public static String base64Encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }
}

// jdk11版本
class NewBase64Decode {
    /**
     * TODO:Decode
     */
    public static byte[] base64Decode(String txt) throws Exception {
        if (StringUtils.isBlank(txt)) {
            return null;
        }
        byte[] bytes = Base64.getDecoder().decode(txt);
        return bytes;
    }

    /**
     * TODO:Encode
     */
    public static String base64Encode(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }
}

要点

【要点】(点击展开)

由于不过于纠结 JDK11 版本升级 弃用删除了哪些方法或者哪些类不再内嵌。最快的方法是直接修改版本后,启动再发现缺失的东西,再进行替换、去掉、或者其他依赖方法进行处理。

  1. 修改 JDK 版本 image.png image.png
  1. 直接启动,等待缺少需要 image.png

总结

本篇文章介绍了, jdk8 升级 jdk11 的一些注意问题。主要围绕-

  1. .sh 脚本启动无法启动问题
  2. javafx.util.Pair 替换
  3. sun.misc.BASE64Decoder 替换
  4. ... (后续碰到会持续更新)

(主要要点:)

文章小尾巴

【文章小尾巴】(点击展开)

文章写作、模板、文章小尾巴可参考:《写作“小心思”》
  感谢你看到最后,最后再说两点~
  ①如果你持有不同的看法,欢迎你在文章下方进行留言、评论。
  ②如果对你有帮助,或者你认可的话,欢迎给个小点赞,支持一下~
   我是南方者,一个热爱计算机更热爱祖国的南方人。
  (文章内容仅供学习参考,如有侵权,非常抱歉,请立即联系作者删除。)

Guess you like

Origin juejin.im/post/7240461753934807101
jdk