Skip to content

Instantly share code, notes, and snippets.

@bigdragon1977
Forked from ChinaXing/java-attach-api.java
Created April 17, 2016 15:22
Show Gist options
  • Save bigdragon1977/1895bfc78c4d7911cd2536af1a7e7a1b to your computer and use it in GitHub Desktop.
Save bigdragon1977/1895bfc78c4d7911cd2536af1a7e7a1b to your computer and use it in GitHub Desktop.
attach to JVM use java attach API
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
import java.util.List;
public class ListVM{
public static void main(String[] args){
List<VirtualMachineDescriptor> vmList = VirtualMachine.list();
for(VirtualMachineDescriptor vm : vmList){
System.out.println("name: " + vm.displayName() + " id :" + vm.id());
try{
VirtualMachine vm0 = VirtualMachine.attach(vm.id());
// load agent, agnet class's agentmain will be invoked.
vm0.loadAgent("/root/tmp/ChinaAgent.jar");
System.out.println("Load agent done.");
vm0.detach();
}catch(Exception e) {
System.out.println("exception : " + e.getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment