我刚刚尝试使用 Java 9 运行我的服务器并收到下一个警告:
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by io.netty.util.internal.ReflectionUtil (file:/home/azureuser/server-0.28.0-SNAPSHOT.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of io.netty.util.internal.ReflectionUtil
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
我想在启动期间隐藏此警告而不添加 --illegal-access=deny
到 JVM 选项。就像是:
System.setProperty("illegal-access", "deny");
有什么办法吗?
所有建议使用 JVM 选项的相关答案,我想从代码中关闭它。那可能吗?
澄清 - 我的问题是关于从代码而不是通过类似问题中所述的 JVM 参数/标志来转换此警告。
原文由 Dmitriy Dumanskiy 发布,翻译遵循 CC BY-SA 4.0 许可协议
有一些方法可以禁用非法访问警告,但我不建议这样做。
1. 简单的方法
由于警告打印到默认错误流,您可以简单地关闭此流并将
stderr
重定向到stdout
。笔记:
System.setErr
来重定向警告消息,因为对错误流的引用保存在IllegalAccessLogger.warningStream
JVM 引导程序早期的字段中。2. 不改变 stderr 的复杂方法
好消息是
sun.misc.Unsafe
仍然可以在 JDK 9 中访问而不会出现警告。解决方案是在不安全 API 的帮助下重置内部IllegalAccessLogger
。