puniverse/quasar — pull request #46 by dcapwell (2014-05-17 06:18:12+00:00)
Title: Added support to sideload the javaagent

With Premain-Class defined I could start the JVM up with -javaagent:quasar.jar, but some times its nicer to pull this in after the JVM is already started (running main in IDE without needing to configure javaagent). This patch adds Agent-Class to the manifest and makes agentmain delegate to premain.

Tested with the following code in my main method:

...
public static void main(String[] args) {
loadAgent();
...
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);
final File agentJar = new File("../quasar/quasar-core/build/libs/quasar-core-0.6.0-SNAPSHOT.jar");
if(!agentJar.exists()) error("Unable to locate agent jar!");
vm.loadAgent(agentJar.getAbsolutePath(), "");
vm.detach();
} catch (Exception e) {
throw new RuntimeException(e);
}
}