VirtualMachine.attach(pid) 失败并出现 java.io.

在经历了这个讨论之后,我相信附加到同一个 VM 的选项默认情况下在 OpenJDK11 中已被禁用。


我正在尝试将 Java 代理升级到 OpenJDK11,在调用时的测试用例中,VirtualMachine.attach(pid)我看到它失败并出现以下错误。处理这种情况的正确方法是什么?


完整的堆栈跟踪:


java.io.IOException: Can not attach to current VM


at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.<init>(HotSpotVirtualMachine.java:75)

at jdk.attach/sun.tools.attach.VirtualMachineImpl.<init>(VirtualMachineImpl.java:48)

at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:69)

at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)

at org.kantega.notsoserial.WithAgentIT.attachAgent(WithAgentIT.java:76)

at org.kantega.notsoserial.WithAgentIT.attackShouldBePreventedWithAgent(WithAgentIT.java:47)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.base/java.lang.reflect.Method.invoke(Method.java:566)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)


幕布斯6054654
浏览 522回答 4
4回答

慕虎7371278

请参阅JDK-8180425:发行说明:默认情况下无法使用附加 API 附加到当前 VM:Attach API 的实现在 JDK 9 中已更改为默认情况下不允许附加到当前 VM。此更改对使用 Attach API 附加到正在运行的 VM 的工具应该没有影响。它可能会影响滥用此 API 作为获取java.lang.instrumentAPI 的方式的库。jdk.attach.allowAttachSelf可以在命令行上设置系统属性以减轻与此更改的任何兼容性。

一只斗牛犬

我不确定这是否对每个人都有帮助,但就我而言,这是一个测试用例,用于测试代理是否正确附加到 JDK(当代理实际附加到 JDK 时它不会自附加,即,实际运行时不是测试用例)。根据@Holger 的建议,在评论中,我修改了我的 maven-failsafe-plugin 以允许自我附加。&nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-failsafe-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.22.2</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>integration-test</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>verify</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <argLine>-Djdk.attach.allowAttachSelf=true</argLine>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <forkMode>once</forkMode>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; </plugin>

狐的传说

使用 JVM 参数:-Djdk.attach.allowAttachSelf=true你可以解决它。

摇曳的蔷薇

这通过在 maven-surefire-plugin 中添加 javaagent 对我有用<plugin>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.maven.plugins</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>maven-surefire-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>${maven-surefire-plugin.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Centralize test reports in parent project -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <reportsDirectory>${basedir}/../target/surefire-reports</reportsDirectory>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Sets the VM argument line used for Jacoco when unit tests are run. -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <argLine>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar ${surefireArgLine}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </argLine>&nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp;</plugin>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java