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.warningStream
field 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 中?
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01