JDK11环境下项目启动时JVM有WARNING信息打印,提示An illegal reflective access operation has occurred
Oracle文章提到:
By default, a type in a module is not accessible to other modules unless it’s a public type and you export its package. You expose only the packages you want to expose. With Java 9, this also applies to reflection.
这意味着JDK9后,开发者没法随心所欲的通过反射调用JDK内部的方法、变量。
可以在JVM参数中添加--add-exports或--add-opens来解决。其中--add-exports可以解决普通的反射,若涉及深层反射如setAccessible(true),则需要使用--add-opens。参数用法:
--add-opens module/package=target-module(,target-module)*
例如
--add-opens java.management/sun.management=ALL-UNNAMED
详细使用方法可以阅读Java Platform, Standard Edition Oracle JDK 9 Migration Guide, Release 9
更加优雅的方法当然是不再通过反射调用JDK内部API,但似乎不是那么容易。在代码中也用了不少三方库,这需要等三方包的开发者修复。