public static void attach(String agentJar, String pid) {
try {
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(agentJar);
vm.detach();
} catch (AttachNotSupportedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (AgentLoadException e) {
e.printStackTrace();
} catch (AgentInitializationException e) {
e.printStackTrace();
}
}
try {
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(agentJar);
vm.detach();
} catch (AttachNotSupportedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (AgentLoadException e) {
e.printStackTrace();
} catch (AgentInitializationException e) {
e.printStackTrace();
}
}
转载于:https://blog.51cto.com/memory/1174695