The Instrumentation API has a hook for installing a bytecode transformer. The transformer must be
installed before the main method of the program is called. You handle this requirement by
defining an agent, a library that is loaded to monitor a program in some way. The agent
code can carry out initializations in a premain method.
Here are the steps required to build an agent:
• Implement a class with a method:
public static void premain(String arg, Instrumentation instr)
This method is called when the agent is loaded. The agent can get a single command-
line argument, which is passed in the arg parameter. The instr parameter can be used to
install various hooks.
• Make a manifest file that sets the Premain-Class attribute, for example:
• Package the agent code and the manifest into a JAR file, for example:
jar cvfm EntryLoggingAgent.jar EntryLoggingAgent.mf *.class
To launch a Java program together with the agent, use the following command-line
java -javaagent:AgentJARFile=agentArgument . . .