news 2026/3/22 20:50:06

JDK升级指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
JDK升级指南

一 JDK升级工具-EMT4J

1.1 工具介绍

EMT4J is a project that aims to simplify the Java version migration. At the moment, this project focuses on three LTS (i.e. Long-Term-Support) versions: 8, 11, 17 and 21. Therefore, if you want to migrate your application running on JDK 8/11/17 to JDK 11/17/21, then this project is for you.

EMT4J supports statically checking application artifacts including the project's classes and dependencies. It also supports running as a Java agent to perform runtime checking. During the checking process, EMT4J collects compatibility problems and outputs a report finally. It currently supports HTML, TEXT, and JSON formats. Users transform the project according to the report and finally complete the migration of the Java version.

Support for Java 21 and Autofix are introduced by version 0.9 which has not been released yet. If you want to use these two features, you need to manually install EMT4J to your local Maven repository.

1.2 工具使用

项目引入maven plugin

<build>

<plugins>

<plugin>

<groupId>org.eclipse.emt4j</groupId>

<artifactId>emt4j-maven-plugin</artifactId>

<version>0.8.0</version>

<executions>

<execution>

<phase>process-test-classes</phase>

<goals>

<goal>check</goal>

</goals>

</execution>

</executions>

<configuration>

<fromVersion>8</fromVersion>

<toVersion>11</toVersion>

<outputFile>report.html</outputFile>

</configuration>

</plugin>

</plugins>

</build>

执行命令

mvn process-test-classes

1.3 EMT4J报告

优先级

含义

风险程度

建议处理方式

P1

高优先级

极可能导致应用启动失败或运行时崩溃(如ClassNotFoundException、调用已删除方法)

必须修复,否则升级后大概率无法运行

P2

中优先级

可能导致功能异常或类加载问题

(如 ContextClassLoader 错误、反射失效)

建议修复,尤其在复杂应用(Spring Boot、Web 容器)中容易出问题

P3

低优先级

可能影响非核心功能,或仅在特定场景下有问题

可暂缓,上线前评估是否影响业务

P4

提示/信息级

通常是代码规范、内部 API 使用警告,不一定导致错误

可选修复,用于长期维护和合规

二 基于EMT4J报告JDK8升级21

2.1 构建新插件

官网提示

Support for Java 21 and Autofix are introduced by version 0.9 which has not been released yet. If you want to use these two features, you need to manually install EMT4J to your local Maven repository.

2.2 EMT4J报告问题修复案例

风险描述

测试用例

import com.xiaoleilu.hutool.lang.JarClassLoader;

import java.io.File;

public class TestJarClassLoader {

public static void main(String[] args) {

// 尝试实例化 JarClassLoader —— 这会触发对 system classloader 的 cast

try {

JarClassLoader.loadJarToSystemClassLoader(new File("test.jar"));

System.out.println("Load success!");

} catch (Exception e) {

e.printStackTrace();

}

}

}

执行结果

javac -cp "hutool-all-3.3.2.jar" TestJarClassLoader.java

java -cp ".:hutool-all-3.3.2.jar" TestJarClassLoader

java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')

at com.xiaoleilu.hutool.lang.JarClassLoader.loadJarToSystemClassLoader(JarClassLoader.java:71)

at TestJarClassLoader.main(TestJarClassLoader.java:6)

升级建议

升级hutool版本

三 附录-Maven插件介绍

3.1 什么是插件

在 Maven 中,插件分为两种类型:构建插件(build plugins) 和 报告插件(reporting plugins)。

构建插件:在项目构建过程中执行,配置在 <build/> 元素中,用途是编译、测试、打包、部署等构建任务

报告插件:在站点(site)生成过程中执行,配置在 <reporting/> 元素中,用途是生成项目文档、代码质量报告、依赖分析等

3.2 Build插件配置

Build插件Mojo开发

Build插件maven配置

3.3 Hello world Build插件

@Mojo(name = "hello")
public class HelloWorldMojo extends AbstractMojo {
@Parameter(property = "msg", defaultValue = "false")
private String msg;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("hello " + msg);
}
}

那么如何集成呢?欢迎评论区留下maven集成配置

四 常见问题

4.1 mvn配置问题

[ERROR] Please make sure you define the required toolchains in your ~/.m2/toolchains.xml file.

[ERROR] jdk [ version='8' ] [ERROR] Please make sure you define the required

解决方案

~/.m2/toolchains.xml

<?xml version="1.0" encoding="UTF-8"?>

<toolchains>

<!-- JDK toolchains -->

<toolchain>

<type>jdk</type>

<provides>

<version>8</version>

<vendor>openjdk</vendor>

</provides>

<configuration>

<jdkHome>/data/opt/jdk1.8.0_191</jdkHome>

</configuration>

</toolchain>

</toolchains>

五 参考链接

https://github.com/adoptium/emt4j

https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Configuring_Build_Plugins

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/21 9:43:58

上海样册设计指南,如何让企业样册脱颖而出

上海样册设计指南&#xff1a;如何让企业样册脱颖而出小编说&#xff1a;在上海这样竞争激烈的商业环境中&#xff0c;一份出色的企业样册不仅是信息的载体&#xff0c;更是品牌无声的代言人。许多企业在寻找设计服务时&#xff0c;常常困惑于如何让自己的样册在众多同类中脱颖…

作者头像 李华
网站建设 2026/3/16 3:56:03

Kotaemon Helm Chart发布:简化云原生部署流程

Kotaemon Helm Chart发布&#xff1a;简化云原生部署流程 在企业加速拥抱AI的今天&#xff0c;一个现实问题始终困扰着技术团队&#xff1a;为什么一个在本地运行良好的智能问答系统&#xff0c;到了生产环境就频频出错&#xff1f;配置不一致、依赖缺失、资源争用……这些“部…

作者头像 李华
网站建设 2026/3/22 12:31:41

Docker 安装 Redis

Docker 安装 Redis 是开发/生产环境中最常用的方式&#xff0c;核心是在线拉取镜像 自定义配置 数据持久化&#xff08;避免容器销毁丢失数据&#xff09;。以下是完整的在线安装步骤&#xff08;适配 Linux/macOS&#xff0c;Windows 仅需微调路径&#xff09;&#xff0c;兼…

作者头像 李华
网站建设 2026/3/15 18:36:26

FFT 工程关键点总结(采样分辨率 / 频点 / 相位)

本文只讨论 FFT 在嵌入式与工程中的实际含义&#xff0c;不涉及数学推导。一、采样分辨率是什么&#xff1f; 1. 先定义所有变量&#xff08;很重要&#xff09;符号含义fs采样率&#xff08;Hz&#xff09;&#xff0c;每秒采样多少次NFFT 点数&#xff08;一次参与 FFT 的采样…

作者头像 李华