获取Java中“-非法访问"设置的当前值 [英] Get the current value of `-illegal-access` setting in Java

查看:909
本文介绍了获取Java中“-非法访问"设置的当前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文 JDK 9:建议允许默认情况下是非法的反射访问 ,声称–permit-illegal-access选项将由更通用的选项–illegal-access代替.

This article, JDK 9: Proposal to allow illegal reflective access by default, claims that the –permit-illegal-access option will be replaced by a more general option, –illegal-access.

  • –illegal-access=permit
  • –illegal-access=warn
  • –illegal-access=debug
  • –illegal-access=deny
  • –illegal-access=permit
  • –illegal-access=warn
  • –illegal-access=debug
  • –illegal-access=deny

➥发生了吗?有–illegal-access设置吗?

➥ Did that happen? Is there a –illegal-access setting?

➥这些设置如何?关于启动JVM的争论?

➥ How are these set? Arguments to launching the JVM?

➥在运行时如何获取当前值?

➥ How does one get the current value during runtime?

推荐答案

发生了吗?有–非法访问设置吗?

是的,看来确实发生了-至少对于OpenJDK/OracleJDK.该选项在java工具"的文档中列出.

Did that happen? Is there a –illegal-access setting?

Yes, it appears that did happen—at least for OpenJDK/OracleJDK. The option is listed in the documentation for the java "tool".

  • JDK-9 documentation
  • JDK-10 documentation
  • JDK-11 documentation

在执行java --help-extra时也会列出.

注意:JDK-11文档提到此选项将在以后的版本中删除.

是的,它是一个命令行选项.示例:

Yes, it is a command-line option. Example:

java --illegal-access=deny --module-path <path> --module <module>/<main-class> [args...]


如何在运行时获取当前值?

不幸的是,我不知道有什么方法可以在运行时查询值.它似乎不是系统或环境属性的一部分.我尝试查找该值在内部使用的位置,但无法使用(但说实话,我并没有花太多时间在查找上).


How does one get the current value during runtime?

Unfortunately, I'm not aware of any way to query the value at runtime. It doesn't appear to be part of the system or environment properties. I tried finding where the value was used internally but was unable to (but to be honest I didn't spend too much time looking).

为方便起见,这是JDK-11的--illegal-access文档:

For convenience, here's the documentation of --illegal-access for JDK-11:

-illegal-access =参数

在运行时出现时,--illegal-access=使用关键字 parameter 指定操作模式:

--illegal-access=parameter

When present at run time, --illegal-access= takes a keyword parameter to specify a mode of operation:

注意:

此选项将在以后的版本中删除.

Note:

This option will be removed in a future release.

  • permit:如果JDK 8中存在该程序包,则此模式将打开运行时映像中每个模块中的每个程序包,以对所有未命名的模块进行编码(例如,类路径中的代码).通过平台的各种反射API的静态访问(例如,通过编译的字节码进行的访问)和深度反射访问.对任何此类包装的第一次反射式访问操作都会发出警告.但是,第一次出现后不会发出警告.此单个警告描述了如何启用进一步的警告.此模式是当前JDK的默认模式,但在以后的版本中会更改.

    • permit: This mode opens each package in each module in the run-time image to code in all unnamed modules ( such as code on the class path), if that package existed in JDK 8. This enables both static access, (for example, by compiled bytecode, and deep reflective access) through the platform's various reflection APIs. The first reflective-access operation to any such package causes a warning to be issued. However, no warnings are issued after the first occurrence. This single warning describes how to enable further warnings. This mode is the default for the current JDK but will change in a future release.

      warn:此模式与permit相同,不同之处在于,每次非法反射访问操作都会发出警告消息.

      warn: This mode is identical to permit except that a warning message is issued for each illegal reflective-access operation.

      debug:此模式与warn相同,只是针对每个非法的反射访问操作均发出警告消息和堆栈跟踪.

      debug: This mode is identical to warn except that both a warning message and a stack trace are issued for each illegal reflective-access operation.

      deny:此模式将禁用所有非法访问操作,但其他命令行选项(例如--add-opens)启用的操作除外.在将来的版本中,此模式将成为默认模式.

      deny: This mode disables all illegal-access operations except for those enabled by other command-line options, such as --add-opens. This mode will become the default in a future release.

      默认模式--illegal-access=permit旨在使您了解类路径上的代码,这些代码至少可以一次反射地访问任何JDK内部API.要了解所有此类访问,可以使用warndebug模式.对于类路径上需要非法访问的每个库或框架,您有两个选择:

      The default mode, --illegal-access=permit, is intended to make you aware of code on the class path that reflectively accesses any JDK-internal APIs at least once. To learn about all such accesses, you can use the warn or the debug modes. For each library or framework on the class path that requires illegal access, you have two options:

      • 如果组件的维护者已经发布了不再使用JDK内部API的固定版本,则可以考虑升级到该版本.

      • If the component's maintainers have already released a fixed version that no longer uses JDK-internal APIs then you can consider upgrading to that version.

      如果仍然需要修复该组件,则可以联系其维护者,并要求他们使用适当的导出API替换其对JDK内部API的使用.

      If the component still needs to be fixed, then you can contact its maintainers and ask them to replace their use of JDK-internal APIs with the proper exported APIs.

      如果必须继续使用需要非法访问的组件,则可以通过使用一个或多个--add-opens选项仅打开那些需要访问权限的内部软件包来消除警告消息.

      If you must continue to use a component that requires illegal access, then you can eliminate the warning messages by using one or more --add-opens options to open only those internal packages to which access is required.

      要验证您的应用程序已准备好用于将来的JDK版本,请使用--illegal-access=deny以及任何必要的--add-opens选项运行它.任何剩余的非法访问错误很可能是由于从编译代码到JDK内部API的静态引用所致.您可以通过运行--jdk-internals选项的"noreferrer> jdeps 工具.出于性能原因,当前的JDK不会针对非法的静态访问操作发出警告.

      To verify that your application is ready for a future version of the JDK, run it with --illegal-access=deny along with any necessary --add-opens options. Any remaining illegal-access errors will most likely be due to static references from compiled code to JDK-internal APIs. You can identify those by running the jdeps tool with the --jdk-internals option. For performance reasons, the current JDK does not issue warnings for illegal static-access operations.

      这篇关于获取Java中“-非法访问"设置的当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆