如何全局设置jdk.attach.allowAttachSelf = true [英] how to set jdk.attach.allowAttachSelf=true globally
问题描述
我正在尝试将我们的系统之一从Java 8迁移到Java 9,并且在Java 8中可以正常运行的单元测试中,大约有三分之一失败,并显示以下错误:
I am trying to move one of our systems from java 8 to java 9 and about one third of the unit tests that worked OK in java 8 fails with the error below:
java.io.IOException: Can not attach to current VM
Google将我带到了几页,我很快了解到,在Java 9中,默认行为已更改,以防止附加到当前VM,并返回到旧方法,需要将系统属性jdk.attach.allowAttachSelf
设置为true
Google took me to a few pages and I quickly understood that in java 9 the default behavior was changed to prevent attaching to the current VM and to go back to the old way you need to set the system property jdk.attach.allowAttachSelf
to true.
在IntelliJ中进行设置时,测试工作正常.更改build.gradle使其包含以下内容时,该方法同样有效:
When setting this in IntelliJ the tests work fine. The same works when changing build.gradle to include this:
test {
jvmArgs '-Djdk.attach.allowAttachSelf=true'
}
但是,我宁愿在全球范围内使用此设置,这样就无需破解我的build.gradle和IntelliJ.
However, I would rather prefer to have this setting globally so I don't need to hack my build.gradle and IntelliJ.
我在ubuntu上运行Java 9,并且更改了/etc/profile.d/jdk.sh
使其包含以下内容:
I am running java 9 on ubuntu and I changed /etc/profile.d/jdk.sh
to include this:
export JDK_JAVA_OPTIONS="-Djdk.attach.allowAttachSelf=true"
在运行Gradle构建时,我可以看到设置被拾取,因为我在构建输出中位于以下位置:
When running my Gradle build I can see the setting being picked up because I am getting below in the build output:
NOTE: Picked up JDK_JAVA_OPTIONS: -Djdk.attach.allowAttachSelf=true
但是,测试始终失败,并带有相同的IOException.
However, the tests keep failing with the same IOException.
那么我在做什么错了,应该如何解决?
So what I am doing wrong and how should I fix it?
预先感谢您的输入.
推荐答案
我的测试正在使用surefire插件运行, 如果在argline中添加了vm参数,则测试开始工作.
My tests were running using surefire plugin, If said vm argument added in argline, then test started working.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<argLine>-Djdk.attach.allowAttachSelf=true </argLine>
</configuration>
</plugin>
这篇关于如何全局设置jdk.attach.allowAttachSelf = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!