使用Attach Api时出错

San*_*oel 9 java instrumentation agent attach-api

在Eclipse中使用attach API时会在控制台中引发错误.

为了使用attach API,我在eclipse中安装了JRE,然后在当前JRE的外部jar选项中使用了包含tools.jar的edit选项.

但是当我尝试执行一个简单的程序时,它会抛出以下错误

java.lang.UnsatisfiedLinkError:java.library.path中没有附件com.sun.tools.attach.AttachNotSupportedException:在dynamicLoadingTest.VMAttach的com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:190)上没有安装任何提供程序.主要(VMAttach.java:17)

这是执行的简单程序.import java.io.IOException;

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

public class VMAttach {


    public static void main(String[] args) {


        try {
            VirtualMachine vm = VirtualMachine.attach("6832");
        } catch (AttachNotSupportedException e) {
            System.out.println("This error");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

提到的进程ID 6832是在系统上运行的Java应用程序

在将安装的jre更改为jdk后,会产生以下错误

Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: jvm.dll not loaded by target process
    at sun.tools.attach.WindowsVirtualMachine.<init>(WindowsVirtualMachine.java:46)
    at sun.tools.attach.WindowsAttachProvider.attachVirtualMachine(WindowsAttachProvider.java:52)
    at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:195)
    at dynamicLoadingTest.VMAttach.main(VMAttach.java:17)
Run Code Online (Sandbox Code Playgroud)

Dee*_*Jha 9

实际上,当你的系统中有不同的jdks并且所有的都包含在类路径中时,这个问题就会出现问题.