How do I pass arguments to a Java instrumentation agent?
Asked Answered
E

1

19

How do I pass arguments to a java.lang.instrument instrumentation agent? The documentation simply states:

-javaagent:jarpath[=options]

What options can I select?

Elegy answered 25/4, 2014 at 7:50 Comment(0)
E
33

To pass arguments to a Java agent, append them after the equals sign:

java -javaagent:/path/to/agent.jar=argumentstring -cp jar-under-test.jar Foo.Main

The arguments are treated as a single string and passed to your premain method. You are responsible for any further processing of the arguments, e.g. splitting on commas or separating key=value pairs.

public static void premain(String agentArgument,Instrumentation instrumentation){
  // args passed in 'agentArgument'
}

Note: if you don't pass any arguments to your agent (i.e. omitting the equals sign), the agentArgument argument will be null, rather than an empty string.

Elegy answered 25/4, 2014 at 7:50 Comment(2)
how can I pass a non-null instrumentation argument?Fulford
instrumentation is provided by the JVM, that is not something that is user-controlled.Thermophone

© 2022 - 2024 — McMap. All rights reserved.