How can I add a Javaagent to a JVM without stopping the JVM?

See Starting a Java agent after program start. It links to http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/ that under “Dynamic loading of a javaagent at runtime” provides working example: public static void loadAgent() throws Exception { String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); String pid = nameOfRunningVM.substring(0, nameOfRunningVM.indexOf(‘@’)); VirtualMachine vm = VirtualMachine.attach(pid); vm.loadAgent(jarFilePath, “”); vm.detach(); } Note that Java 9 requires -Djdk.attach.allowAttachSelf=true to … Read more

Using Instrumentation to record unhandled exception

I think you should use ASM to manipulate bytecode directly. Here is algoritms: Visit all try/catch blocks (see visitTryCatchBlock) and save all handler labels Visit instructions until one of the handler labels met. After handler label insert logging code GETSTATIC java/lang/System out LDC “exception X occured” INVOKEVIRTUAL java/io/PrintStream println (java/lang/String)V And ensure that your javaagent … Read more

Tutorials about javaagents [closed]

The second case talks about Java Instrumentation API – this link points to a Javadoc which is rather descriptive. And here, is the full instruction and an example of how to create java instrumentation agent. The main concept is to: Implement a static premain (as an analogy to main) method, like this: import java.lang.instrument.Instrumentation; class … Read more