I have created a java agent jar file (which works perfect on command line). Next I would like to attach this agent to a running JVM in a j2ee server. So I used the code:
public static void loadAgent() {
System.out.println("dynamically loading javaagent");
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
String pid = nameOfRunningVM.substring(0, p);
try {
VirtualMachine vm = VirtualMachine.attach(pid);
String jarFilePath = vm.getSystemProperties().getProperty("java.home")+File.separator+"lib"+File.separator+"test-agent-7.jar";
vm.loadAgent(jarFilePath, "");
vm.detach();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
EDIT: This is now working, and I am able to see that Transformer is called. I can also remove the transformers and been able to debug the transformers.
My question is:
- How do i Unload this java agent jar once finished?
- Is the only way to shutdown/restart the JVM?
- Is there any impact if the jar is left in memory?