4

I'm having troubles to attach my java app to a remote java vm. I'm using java 8u45 on windows 7 and my attaching code is very simple

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import com.sun.tools.attach.spi.AttachProvider;

public class AgentLoader
{
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        String pid = s.next();

        try
        {
            VirtualMachine vm = VirtualMachine.attach(pid);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

when attach being called, I get AttachNotSupportedException and that there's no provider installed. I looked through some answers on the web and I saw few solutions that didn't work for me. like making sure that I don't mix up few versions of java on the same machine. set PATH variable to point to the attach.dll library located in %JAVA_HOME%\jre\bin etc..

none of them worked for me

any idea what could be the issue? Thanks

stylo
  • 496
  • 4
  • 12
  • By remote do you mean something on the same machine or a different one ? – Akash Yadav May 19 '15 at 13:15
  • In the same machine but in a different java process – stylo May 19 '15 at 13:21
  • And you are passing the Correct PID for the running process , i tried the same code and passed the pid of eclipse running , i could see it getting attached – Akash Yadav May 19 '15 at 13:22
  • Yes, the process id is the same as the desired process. keep in mind that I don't want to attach it to my own process (running from eclipse) but to another java process – stylo May 19 '15 at 13:44
  • can you provide the full stack trace ? – Akash Yadav May 20 '15 at 10:21
  • stack trace sun.misc.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:190) at AgentLoader.main(AgentLoader.java:19) – stylo May 20 '15 at 11:06
  • well, I'v figured that the problem only occurs when debugging the process using eclipse. – stylo May 20 '15 at 11:58
  • lmfao, i have the same agent class name. – Joe May 25 '18 at 00:01

2 Answers2

1

Include the following lines of code and it should work. It requires the attach.dll file to work.

static
{
   System.loadLibrary("attach");
}
Lol
  • 11
  • 2
1

Change your IDE's jre dependency from jre to jdk. As for eclipse, Window -> Preferences -> Java -> Installed JREs, replace jre with jdk.

yun xu
  • 11
  • 2