How to hide warning quot;Illegal reflective accessquot; in java 9 without JVM argument?(如何隐藏警告“非法反射访问在没有 JVM 参数的 java 9 中?)
问题描述
我刚刚尝试使用 Java 9 运行我的服务器并收到下一个警告:
I just tried to run my server with Java 9 and got next warning:
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 选项.比如:
I would like to hide this warning without adding --illegal-access=deny to JVM options during start. Something like:
System.setProperty("illegal-access", "deny");
有什么办法吗?
建议使用 JVM 选项的所有相关答案,我想从代码中关闭它.这可能吗?
All related answers suggesting to use JVM options, I would like to turn off this from code. Is that possible?
澄清一下 - 我的问题是关于从代码中转换此警告,而不是通过类似问题中所述的 JVM 参数/标志.
To clarify - my question is about turning this warning from the code and not via JVM arguments/flags as stated in similar questions.
推荐答案
有一些方法可以禁用非法访问警告,但我不建议这样做.
There are ways to disable illegal access warning, though I do not recommend doing this.
由于警告打印到默认错误流,您可以简单地关闭此流并将 stderr 重定向到 stdout.
Since the warning is printed to the default error stream, you can simply close this stream and redirect stderr to stdout.
public static void disableWarning() {
System.err.close();
System.setErr(System.out);
}
注意事项:
- 这种方法合并了错误流和输出流.在某些情况下,这可能是不可取的.
- 您不能仅通过调用
System.setErr来重定向警告消息,因为对错误流的引用在 JVM 引导的早期保存在IllegalAccessLogger.warningStream字段中.
- This approach merges error and output streams. That may not be desirable in some cases.
- You cannot redirect warning message just by calling
System.setErr, since the reference to error stream is saved inIllegalAccessLogger.warningStreamfield early at JVM bootstrap.
一个好消息是 sun.misc.Unsafe 仍然可以在 JDK 9 中访问而不会出现警告.解决方案是借助 Unsafe API 重置内部 IllegalAccessLogger.
A good news is that sun.misc.Unsafe can be still accessed in JDK 9 without warnings. The solution is to reset internal IllegalAccessLogger with the help of Unsafe API.
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
}
}
这篇关于如何隐藏警告“非法反射访问"在没有 JVM 参数的 java 9 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何隐藏警告“非法反射访问"在没有 JVM 参数的 java 9 中?
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
