premain is called when the agent starts before the application. Agents called with premain are specified using the -javaagent switch.
agentmain is called when the agent starts after the application is already running. Agents starting with agentmain can be attached programmatically using the Sun tool API (Sun / Oracle JVM only - the method for introducing dynamic agents is implementation dependent).
An agent can have both premain and agentmain , but only one of them will be called in a specific JVM call. In other words, your agent will start with premain or agentmain , but not both.
Further information on this can be found in the answer to the question Starting Java Agent after starting the program .
source share