2

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
  • Please use e.printStackStrace() instead of e.getMessage(). Message returns only last clue whereas there can be the root cause in the full stacktrace. Commented Aug 24, 2020 at 15:29
  • Still gives me the same exception with no further details. Commented Aug 25, 2020 at 7:48

2 Answers 2

1

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) 
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I have tried this before and got this error. The problem here that the application does not work properly on jdk 6. some elements throw errors as I guess they don't exist in jdk 6.
Maybe that would be worth investigating further. JDK 6 should be backwards compatible to JDK 5. Is there any chance you can create a new question and post the errors you are getting? Maybe it is a JDK incompatibility, but chances are you rather have a configuration problem. Besides, what about recompiling the source code on a more recent JDK 6+? If you could share source code (maybe on GitHub) for that old project in your new question, that would help others investigate your problem. Feel free to also post a link to the new question here.
Thanks @kriegaex. Unfortunately the application is not mine. I will contact the vendor to check with them the ability to recompile the code with jdk 6+.
If the vendor refuses to do that for you, then depending on what kind of agent you want to use and how static or dynamic the woven-in functionality ought to be, another workaround might be to statically weave the original byte code and create new JAR (or whatever archive) versions of said application, replacing the originals. For instrumentation tools like ByteBuddy and ASM there are Maven plugins (for BB also Gradle) which can take care of build-time instrumentation for you.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.