Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

java - From where does the Attaching Error come?

When I'm Executing my Attacher I will get an Error, and I don't know Why (The Classes are like they Should be)

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 me.paul.TestInjector.Util.attach(Util.java:42)
        at me.paul.TestInjector.Main.main(Main.java:17)

Line 42 in Util: VirtualMachine virtualMachine = VirtualMachine.attach(pid); Line 17 in Main (The Method attach gets Executed) Util.attach(pid, file);

The Attach Method:

public static void attach(String pid, File input) {
    System.setProperty("java.libary.path", System.getProperty("java.home").replace("jre", "jdk") + "\jre\bin");

    try {
        Field field = ClassLoader.class.getDeclaredField("sys_paths");
        field.setAccessible(true);
        field.set(null, null);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
        return;
    }

    File agentFile = new File(System.getProperty("user.home") + "/Desktop", "agent.jar");

    buildAgent(input, agentFile);

    try {
        VirtualMachine virtualMachine = VirtualMachine.attach(pid);

        virtualMachine.loadAgent(agentFile.getAbsolutePath());
        System.out.println("[Test-Injection] Agent loaded");

        virtualMachine.detach();
        System.out.println("[Test-Injection] detached!");
    } catch (IOException | AttachNotSupportedException | AgentLoadException | AgentInitializationException e) {
        e.printStackTrace();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

no providers installed simply means that the current VM does not support attachment. This typically occurs on a JRE (Java 8 and before) or if the instrumentation module is not present (Java 9 and later).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...