--add-opens我最近迁移到 Java 17,由于运行应用程序时的一个依赖项,它带来了一些限制,要求我使用。
我需要在java -jar运行命令时添加它。现在我找到了这些解决方案:
java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/sun.util.calendar=ALL-UNNAMED -jar my.jar
Run Code Online (Sandbox Code Playgroud)
pom.xml<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Add-Opens>java.base/sun.util.calendar java.base/java.util</Add-Opens>
</manifestEntries>
</archive>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
显然,两者都适合生产。然而,当通过 IntelliJ 运行我的应用程序时,它没有选择我认为是正常的选项。我必须将它们设置在我的运行配置中(顺便说一下,它也提交给我的项目)作为虚拟机参数。
我正在寻找一种方法来自动确保一致性,而不必在我声明附加打开的两个地方并行维护。
编辑:我想知道 argfiles 是否可行。就像我的项目中有一个 argfile 一样,它将在 jar 中引用,并且可以在 y 运行配置中引用。我还没有找到太多证据,但这就是我目前正在追求的道路。
编辑2:我addopens在项目的根目录添加了一个文件,现在可以从我需要它的各个点引用它。对于测试,我添加了这个,并且它与 IntelliJ 测试和 Maven 测试一起开箱即用:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- This adds the options contained in the addopens file to the test JVM arguments -->
<argLine>@addopens @{argLine}</argLine>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我还可以将该addopens …
我正在将生产代码中的 java 版本从 java 8 升级到 java 11。
由于使用了flume、zookeeper等第三方库,我必须在应用程序java start命令中添加以下JDK模块配置。
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED
添加此配置后,java 应用程序启动正常。
但是当我使用mvn test测试运行测试时失败了。我已将以下配置添加到 maven-surefire-plugin 中,但它仍然抛出错误。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>--illegal-access=permit</argLine>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<argLine>--add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED</argLine>
<argLine>-Dillegal-access=permit</argLine>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
我想我没有在 maven 测试中正确地传递参数。知道我做错了什么以及如何解决这个问题吗?