I have a java application that must run with jdk 1.5. I need a way to attach to this application JVM using its PID. I tried ByteBuddy library but it is giving me the following error when trying to load the agent.
Exception in thread "main" java.lang.IllegalStateException: Target could not dispatch command successfully
at net.bytebuddy.agent.VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe.execute(VirtualMachine.java:1043)
at net.bytebuddy.agent.VirtualMachine$ForHotSpot.load(VirtualMachine.java:361)
at net.bytebuddy.agent.VirtualMachine$ForHotSpot.loadAgent(VirtualMachine.java:335)
at main.Agent.main(Agent.java:28)
Here's the code in the main method:
public static void main(String[] args) {
try {
VirtualMachine vm = VirtualMachine.ForHotSpot.attach("19708");
vm.loadAgent("Agent.jar");
vm.detach();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Can anyone help me with this issue?
The Attach API was introduced in JDK 6. You find it in package
com.sun.tools.attach, which is unavailable in JDK 5. Also the corresponding libraryjre/lib/.../libattach.so(UNIX-like OS) orjre/bin/attach.dll(Windows) is unavailable in your JDK folder, if you want to compare JDK 5 against 6+. Hence, you cannot hot-attach an agent with this method to a Java 5 VM. Maybe you could run your Java 5 application on a Java 6+ VM and then attach to that one.P.S.: You don't need ByteBuddy in order to hot-attach an agent, see this tutorial:
When running this against a program running in a Java 5 VM, you will also see a more specific error message: