如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?

2022-01-16 00:00:00 jvm java javaagents

我希望在不停止应用程序的情况下分析 Java 应用程序.我可以在应用程序运行时以某种方式添加 Javaagent 吗?

I wish to profile a Java application without stopping the application. Can I add a Javaagent somehow while the application is running?

推荐答案

根据java.lang.instrument 包.

虚拟机启动后启动代理

一个实现可能会提供一种机制来在某个时候启动代理虚拟机启动后.这关于如何启动的详细信息是特定于实现的,但通常应用程序已经开始了,它的主要方法有已经被调用了.在这种情况下一个实现支持VM 启动后代理的启动开始以下适用:

An implementation may provide a mechanism to start agents sometime after the the VM has started. The details as to how this is initiated are implementation specific but typically the application has already started and its main method has already been invoked. In cases where an implementation supports the starting of agents after the VM has started the following applies:

1.agent JAR的manifest必须包含属性代理级.这个的价值属性是代理的名称上课.
2.代理类必须实现一个公共静态agentmain方法.
3.系统类加载器(ClassLoader.getSystemClassLoader)必须支持添加代理的机制JAR 文件到系统类路径.

1.The manifest of the agent JAR must contain the attribute Agent-Class. The value of this attribute is the name of the agent class.
2. The agent class must implement a public static agentmain method.
3. The system class loader ( ClassLoader.getSystemClassLoader) must support a mechanism to add an agent JAR file to the system class path.

但我从未尝试过 :-|

相关文章