首页 > 解决方案 > 无法附加到 VM:com.sun.tools.attach.AttachNotSupportedException:未安装提供程序

问题描述

我正在尝试在正在运行的进程上放置一个 java 代理。我的问题是我无法附加到 VM,因为我收到以下错误:

java.util.ServiceConfigurationError: com.sun.tools.attach.spi.AttachProvider: Provider sun.tools.attach.WindowsAttachProvider could not be instantiated
com.sun.tools.attach.AttachNotSupportedException: no providers installed
    at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:203)
    at io.github.giantnuker.tailor.transform.JavaAgent.attachAgent(JavaAgent.java:31)

重要的部分似乎是com.sun.tools.attach.AttachNotSupportedException: no providers installed

这是产生错误的类:

public class JavaAgent {
    public static void agentmain(String stringArguments, Instrumentation instrumentation) {
        System.out.println("hi from agentmain");
    }

    private static String getPid() {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String pid = bean.getName();
        if (pid.contains("@")) {
            pid = pid.substring(0, pid.indexOf("@"));
        }
        return pid;
    }

    public static void attachAgent() {
        try {
            System.out.println("PID is " + getPid());
            VirtualMachine vm = VirtualMachine.attach(getPid());
            Properties props = vm.getSystemProperties();
            vm.loadAgent(new File(JavaAgent.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath());
            vm.detach();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    static {
        System.loadLibrary("attach");
    }
    public static void main(String[] args) {
        attachAgent();
    }
}

该课程的第 31 行是VirtualMachine vm = VirtualMachine.attach(getPid());

有什么线索吗?谢谢你的时间!

标签: javainstrumentationjavaagents

解决方案


推荐阅读