/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

Spring Boot 警告:An illegal reflective access operation has occurred

* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* @author Alan
* @Email no008@foxmail.com

 

正文

Spring Boot 警告:An illegal reflective access operation has occurred

问题描述

最新将Spring Boot项目升级到JDK 11,运行时发现警告如下:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils (file:/D:/Android/.gradle/caches/modules-2/files-2.1/org.springframework/spring-core/5.2.0.RELEASE/e0e1b3c304f70ed19d7905975f6f990916ada219/spring-core-5.2.0.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

20191021205420.png

 

解决方案

 

方案一

增加 JVM 启动参数:

java --illegal-access=deny

 

Java 9 中这个参数默认是:permit

20191021205515.png

 

方案二

在启动类增加以下方法,亲测试 jdk 11 有效

复制代码
public static void disableWarning() {
    try {
        Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
        theUnsafe.setAccessible(true);
        Unsafe u = (Unsafe) theUnsafe.get(null);

        Class cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
        Field logger = cls.getDeclaredField("logger");
        u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
    } catch (Exception e) {
        // ignore
    }
}
复制代码

 

启动 Spring Boot 时调用一下上面的方法

public static void main(String[] args) {
    disableWarning(); //禁用警告
    SpringApplication.run(AppApplication.class, args);
}

 

 

参考

参考:
https://stackoverflow.com/questions/46454995/how-to-hide-warning-illegal-reflective-access-in-java-9-without-jvm-argument
https://blog.csdn.net/qq_25958497/article/details/87258748
https://stackoverflow.com/questions/53790182/get-the-current-value-of-illegal-access-setting-in-java

 

处理“Error:(11, 25) java: 程序包jdk.internal.misc不存在” 的问题

方案一: 把jdk 切换为 1.8版本

 

 

 

 

 方案二:

编译时添加参数
--add-exports java.base/jdk.internal.misc=ALL-UNNAMED
运行时添加参数
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED


 

 
 
 
 
 
 
 
posted @   一品堂.技术学习笔记  阅读(2140)  评论(1编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
历史上的今天:
2018-03-27 Oracle 数据备份与恢复
2018-03-27 优化SQL语句
点击右上角即可分享
微信分享提示

目录导航