Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass arguments to a Java instrumentation agent?

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

-javaagent:jarpath[=options]

What options can I select?

like image 652
Duncan Jones Avatar asked Apr 25 '14 07:04

Duncan Jones


People also ask

What are the responsibilities of an agent in Java?

Java agents are part of the Java Instrumentation API. The Instrumentation APIs provide a mechanism to modify bytecodes of methods. This can be done both statically and dynamically. This means that we can change a program by adding code to it without having to touch upon the actual source code of the program.

What is Javaagent option?

Java agents are a special type of class which, by using the Java Instrumentation API, can intercept applications running on the JVM, modifying their bytecode.


1 Answers

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.

like image 118
Duncan Jones Avatar answered Sep 18 '22 14:09

Duncan Jones