我只是试图与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参数/标志。
有办法禁止非法访问的警告,但我不建议这样做。
由于报警打印到默认的错误流,你可以简单地关闭该流并重定向stderr
到stdout
。
public static void disableWarning() {
System.err.close();
System.setErr(System.out);
}
笔记:
System.setErr
,因为到错误流参考保存在IllegalAccessLogger.warningStream
早期的JVM引导场。一个好消息是,sun.misc.Unsafe
在JDK 9仍然没有访问警告。解决的办法是重新设定内部IllegalAccessLogger
不安全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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句