Saturday, 3 September 2011

Javaagent

Since JDK 5 we have possibility to run Java application with parameter -javaagent, that allows to intercept compiled code with our own one. Our code is executed in the same JVM, loaded with system class loader with the same security policy.

To create your java agent you must implement method:
public static void premain(String agentArgs, Instrumentation inst);

and you have to archive this class into a jar file, that contains also file META-INF/MANIFEST.MF, with additional attribute Premain-Class with value of class that will be intercepted.
Then just run your application adding parameter -javaagent:your-jar-file.jar

No comments: