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.
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 ofpremain
andagentmain
. – Foundling