Cannot Attach to JVM using jdk 1.5

1.4k Views Asked by At

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?

2

There are 2 best solutions below

4
kriegaex On

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 library jre/lib/.../libattach.so (UNIX-like OS) or jre/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:

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

import java.io.File;
import java.io.IOException;

class Scratch {
  public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
    VirtualMachine jvm = VirtualMachine.attach("22384");
    jvm.loadAgent(new File("foo.jar").getAbsolutePath());
    jvm.detach();
  }
}

When running this against a program running in a Java 5 VM, you will also see a more specific error message:

Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: The VM does not support the attach mechanism
    at sun.tools.attach.HotSpotAttachProvider.testAttachable(HotSpotAttachProvider.java:162)
    at sun.tools.attach.WindowsAttachProvider.attachVirtualMachine(WindowsAttachProvider.java:67)
    at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
    at Scratch.main(scratch.java:11)
0
Rafael Winterhalter On

From the stack trace, I can see that you are using VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe. This means Byte Buddy is emulating the attachment for you using JNA. While this is possible in Java 5 which does not offer an attachment provider, the target VM must be of at least version 6 where dynamic attachment was introduced to dispatch the command to attach.

It is not possible to attach to a version 5 VM, only from one if JNA is available.