虚拟机.attach(pid)失败,并出现java.io. IO异常:无法连接到当前虚拟机

e4eetjau  于 2023-02-28  发布在  Java
关注(0)|答案(5)|浏览(453)

在讨论了this之后,我相信连接到同一个虚拟机的选项在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)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
eqfvzcg8

eqfvzcg81#

参见JDK-8180425 : Release Note: Attach API cannot be used to attach to the current VM by default
在JDK 9中,附加API的实现已更改为默认情况下不允许附加到当前VM。此更改不会影响使用附加API附加到正在运行的VM的工具。它可能会影响误用此API作为获取java.lang.instrument API的方法的库。可以在命令行上设置系统属性jdk.attach.allowAttachSelf,以降低与此更改的任何兼容性。

uplii1fm

uplii1fm2#

我不确定这是否对每个人都有帮助,但在我的例子中,这是一个测试代理是否正确连接到JDK的测试用例(当代理实际连接到JDK时,它不是自连接,即实际运行时不是测试用例)。
基于@Holger的建议,在评论中,我修改了我的maven故障安全插件,以允许自我连接。

<plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <argLine>-Djdk.attach.allowAttachSelf=true</argLine>
                        <forkMode>once</forkMode>
                    </configuration>
                </execution>
            </executions>
        </plugin>
xkrw2x1b

xkrw2x1b3#

我在maven-surefire-plugin中添加了javaagent,这对我很有效

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
        <configuration>
          <!-- Centralize test reports in parent project -->
          <reportsDirectory>${basedir}/../target/surefire-reports</reportsDirectory>
          <!-- Sets the VM argument line used for Jacoco when unit tests are run. -->
          <argLine>
             -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar ${surefireArgLine}
          </argLine>
        </configuration> 
</plugin>
h79rfbju

h79rfbju4#

@太阳猫
使用JVM参数:-Djdk.附加.允许附加自身=真
你就能解决它。

9njqaruj

9njqaruj5#

在我的例子中,我在编译器设置为Jdk11(阿苏尔zulu)时使用以下设置编译我的Android项目。将JDK更改为jdk1.8修复了这个问题。

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

相关问题