news 2026/10/14 23:03:46

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/10/15 4:45:40

使用VirtualBox安装国产麒麟桌面系统

前言 VirtualBox的基础操作参考以下链接。其实&#xff0c;我并不知道是否可行&#xff0c;毕竟当前国产麒麟系统相当小众&#xff0c;因此才有本篇文章。通过查看麒麟系统相关信息&#xff0c;我认为大概率可行。 VirtualBox&#xff1a;看这一篇就够了-CSDN博客 一、快速开…

作者头像 李华
网站建设 2026/10/15 3:33:50

Kotaemon在环境保护科普宣传中的作用

Kotaemon在环境保护科普宣传中的作用 在环境问题日益受到公众关注的今天&#xff0c;如何让复杂的生态知识走出实验室和政策文件&#xff0c;真正走进大众的生活&#xff0c;成为一道亟待解决的现实课题。人们不再满足于被动接收“请节约用水”这样的口号式宣传&#xff0c;而是…

作者头像 李华
网站建设 2026/10/14 23:33:47

【AI平台核心架构设计】

AI平台核心架构设计 知识管理层设计要点 知识管理层采用模块化设计&#xff0c;各功能模块通过统一API网关进行交互。案例库采用版本化存储&#xff0c;支持语义检索和相似度匹配。业务领域知识通过知识图谱进行关联&#xff0c;实现跨领域查询。API目录集成Swagger/OpenAPI规范…

作者头像 李华
网站建设 2026/10/14 18:37:48

向量数据库常用SQL语句

向量数据库常用SQL语句 创建包含向量字段的表 CREATE TABLE products (id SERIAL PRIMARY KEY,name VARCHAR(100),description TEXT,embedding VECTOR(1536) -- 假设使用1536维向量 );插入向量数据 INSERT INTO products (name, description, embedding) VALUES (智能手机, 高…

作者头像 李华
网站建设 2026/10/15 13:12:23

政务热线智能化改造案例:Kotaemon的实际成效

政务热线智能化改造案例&#xff1a;Kotaemon的实际成效 在城市治理日益数字化的今天&#xff0c;政务热线作为政府与公众之间最直接的沟通窗口&#xff0c;正承受着前所未有的压力。某市12345热线平台数据显示&#xff0c;日均来电量已突破两万通&#xff0c;其中近七成是重复…

作者头像 李华
网站建设 2026/10/14 21:37:07

KotaemonLeetCode刷题伴侣:思路提示与优化建议

KotaemonLeetCode刷题伴侣&#xff1a;思路提示与优化建议 在算法学习的征途上&#xff0c;几乎每个开发者都曾经历过这样的时刻&#xff1a;面对一道中等难度的LeetCode题目&#xff0c;脑海中闪过几个模糊的想法&#xff0c;却始终无法串联成完整的解法&#xff1b;翻看题解又…

作者头像 李华