如何在没有 JAR 文件的情况下附加代理?

huangapple go评论66阅读模式
英文:

How to attach agent without a jar?

问题

(使用Java 11)
我可以在运行时通过jar注入代理,如下所示:

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

有没有一种方法可以在没有jar文件的情况下进行附加?我可以使用byte-buddy来实现,但如何在没有第三方库(如byte-buddy)的情况下实现呢?

英文:

(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 ?

答案1

得分: 2

你必须拥有一个用于此的 JAR 文件。

Byte Buddy 代理(byte-buddy-agent,而不是 byte-buddy 的 JAR)包含代理清单头本身。在首次尝试中,它会尝试定位自己的 JAR 文件在本地文件系统中(通过 getProtectionDomain().getCodeSource())。如果无法执行此操作,或者存在于文件夹中作为非 JAR 文件,它将创建一个临时 JAR 文件,并附带所需的属性。

byte-buddy-agent 是一个精简的、零依赖的 JAR。我建议将其用作命名空间,以便于附加 API 在旧的 IBM 和 HotSpot VM 上有所不同,它在 Java 9+ 上需要不同的设置,而且由于 Byte Buddy 可以在存在 JNA 的非 JDK VM 上模拟附加。(免责声明:我是作者。)

英文:

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.)

huangapple
  • 本文由 发表于 2020年8月22日 03:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63528778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定