Question

I done some java bytecode instrumentation with -javaagent argument and premain method. But this is the first time i hear about agentmain method. I have some questions about this method. Here follows it.

Both premain and agentmain method have same use?
When agentmain method invoked?
What is the use of agentmain method in java instrumentation?

Was it helpful?

Solution

premain is invoked when an agent is started before the application. Agents invoked using premain are specified with the -javaagent switch.

agentmain is invoked when an agent is started after the application is already running. Agents started with agentmain can be attached programatically using the Sun tools API (for Sun/Oracle JVMs only -- the method for introducing dynamic agents is implementation-dependent).

An agent can have both a premain and an agentmain, but only one of the two will be called in a particular JVM invocation. In other words, your agent will either start with premain or agentmain, but not both.

You can find more information about this in the answer to the question Starting a Java agent after program start.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top