如何用多个 Java 代理启动 Java 程序?

我知道如何用 java 代理启动 java 程序:

java -javaagent:myAgent.jar MyJavaProgram

但是,如果我想添加2个或更多的 Java 代理来检测我的程序,该怎么办呢?我不希望为 JVM 中必须加载的每个代理重新调用 java-javaagent: ... 。

我试过这样的方法:

java -javaagent:agentA.jar, agentB.jar MyJavaProgram

或者类似这样的东西:

java -javaagent:agentA.jar agentB.jar MyJavaProgram

但是没有成功。

有办法解决我的问题吗?

谢谢你。

55620 次浏览

It would appear you can do this by using multiple arguments. From the documentation:

On implementations with a command-line interface, an agent is started by adding this option to the command-line:

-javaagent:jarpath[=options]

jarpath is the path to the agent JAR file. options is the agent options. This switch may be used multiple times on the same command-line, thus creating multiple agents. More than one agent may use the same jarpath. An agent JAR file must conform to the JAR file specification.

(my emphasis)

How about two javaagent parameters?

java -javaagent:agentA.jar -javaagent:agentB.jar MyJavaProgram

Adding to the above answers, if you are using ant and want to include <jvmargs /> with more than one jar to -javaagent to start the server, here's how I did it,

build.xml

<target name="blah">
...
<jvmarg value="-javaagent:${jar1.path}" />
<jvmarg value="-javaagent:${jar2.path}" />
...
</target>

There is a new project with the goal to support multiple Java agents. Currently it is limited to specific ones.

Agent Bond is a super agent, which wraps and dispatches on several other agents. That way, you only have to install a single agent within your JVM with a single set of configuration data (which contains multiple separate parts).

See https://github.com/fabric8io/agent-bond/blob/master/README.md for details