Java Language Setting up a basic agent


Example

The Premain class will contain the method "premain(String agentArgs Instrumentation inst)"

Here is an example:

import java.lang.instrument.Instrumentation;

public class PremainExample {
    public static void premain(String agentArgs, Instrumentation inst) {
        System.out.println(agentArgs);
    }
}

When compiled into a jar file open the Manifest and ensure that it has the Premain-Class attribute.

Here is an example:

Premain-Class: PremainExample

To use the agent with another java program "myProgram" you must define the agent in the JVM arguments:

java -javaagent:PremainAgent.jar -jar myProgram.jar