What is the use of agentmain method in Java tools

I made several java bytecode tools with the argument -javaagent and premain . But this is the first time I hear about the agentmain method. I have some questions about this method. This implies.

Do both premain and agentmain methods have the same use?
When is the agentmain method called?
What is the use of agentmain method in Java tools?

+6
source share
1 answer

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 .

+8
source

Source: https://habr.com/ru/post/957411/


All Articles