javajarjvmagentbyte-buddy

How to attach agent without a jar?


(Using Java 11)
I could inject agent at runtime with jar, via:

VirtualMachine jvm = VirtualMachine.attach(jvmPid);
jvm.loadAgent("/path/to/agent.jar");
jvm.detach();

Is there any way to attach without a jar ?
I can do that With byte-buddy, but how to do it without 3rd party libraries like byte-buddy ?


Solution

  • You must have a jar file for this.

    The Byte Buddy agent (byte-buddy-agent, not byte-buddy jar) contains the agent manifest header itself. In a first attempt, it tries to locate its own jar on the local file system (via getProtectionDomain().getCodeSource()). If it cannot do this or exists as a non-jar file in a folder, it creates a temporary jar file with the required properties.

    byte-buddy-agent is a minimal, zero-dependency jar. I'd recommend using it as namespaces for the attach API differ on old IBM and HotSpot VMs, it requires different setup on Java 9+ and since Byte Buddy can emulate attachment on non-JDK VMs if JNA is present. (Disclaimer: I am the author.)