Spring Boot项目使用到JDK 11,运行时发现警告如下:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.baomidou.mybatisplus.core.toolkit.SetAccessibleAction (file:/Q:/Jlfw_maven/repository/com/baomidou/mybatis-plus-core/3.4.3/mybatis-plus-core-3.4.3.jar) to field java.lang.invoke.SerializedLambda.capturingClass
WARNING: Please consider reporting this to the maintainers of com.baomidou.mybatisplus.core.toolkit.SetAccessibleAction
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 

虽说不影响运行但是还是觉得碍眼!!!

报错原因:目前知道的是JDK版本过高导致的

解决方案

  • 方案一
    增加 JVM 启动参数:

 java --illegal-access=deny

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

  • 方案二

在启动类增加以下方法,亲测试 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 时调用一下上面的方法 (spring启动类)

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

         

  • 方案三

  降低JDK版本

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐