What is the use of agentmain method in java instrumentation
Asked Answered
C

1

12

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?

Counterpoint answered 5/11, 2013 at 9:54 Comment(1)
stackoverflow.com/questions/19394646/#19400008 shows a sample application which starts an agent in a running JVM through the attach API. In this example it starts the JMX agent if it is not already running. So its agentmain method would be invoked then. This can be adapted to other agents. docs.oracle.com/javase/7/docs/api/java/lang/instrument/… explains the meaning of premain and agentmain.Foundling
S
16

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.

Snooty answered 5/11, 2013 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.