当前位置: 首页 > news >正文

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

http://www.cnnetsun.cn/news/128008.html

相关文章:

  • 自动化营销有哪些方式,国内外有哪些自动化营销工具?
  • Cursor快捷键大全:效率翻倍的隐藏技巧
  • 【项目实战】md 是标准纯文本标记语言,mdx 是其扩展格式(融合 JSX/组件能力)
  • 2、网络指南:印刷版与在线版的选择及网络知识介绍
  • Kotaemon如何处理歧义问题?上下文消解策略解析
  • 6、网络配置与管理全解析
  • 零代码训练!用本地大模型实现文本情感分析
  • Kotaemon备份与恢复策略:防止数据丢失
  • 批量将 Word 文档重命名为其标题
  • Kotaemon本地部署教程:保护数据隐私的新选择
  • Kotaemon支持GraphQL接口吗?现代API集成方案
  • 基于Kotaemon的政策法规智能查询系统
  • Kotaemon前缀缓存机制:加速重复查询响应
  • 42、数据绑定中的错误处理与ASP.NET数据绑定实践
  • 46、WinFx数据绑定入门指南
  • Kotaemon危机公关声明撰写:负面舆情应对
  • Kotaemon如何生成参考文献?学术写作辅助新玩法
  • 12、深入解析词法分析与语法分析工具的核心功能
  • 13、Bison 解析器的高级特性与使用技巧
  • Kotaemon中的元数据过滤功能如何精准定位内容?
  • 部署稳定、效果可追踪——Kotaemon RAG框架核心优势
  • Kotaemon签证政策实时查询系统
  • 请编写一个 Shell 脚本监控系统的 CPU 使用率(中等)
  • SpringBoot+Vue html+css在线英语阅读分级平台管理平台源码【适合毕设/课设/学习】Java+MySQL
  • Kotaemon在制造业的应用探索:设备故障智能诊断
  • Kotaemon支持OAuth2.0认证吗?第三方登录集成
  • 7、macOS Stacks:高效管理与便捷操作指南
  • 11、Mac 系统窗口管理利器:Magnet 与 BetterSnapTool 全解析
  • 12、macOS 键盘自定义与截图技巧全攻略
  • Kotaemon包装文案写作:吸引消费者眼球