Last active
September 13, 2017 08:10
-
-
Save ChinaXing/6044ef2da3cb7075264c to your computer and use it in GitHub Desktop.
attach to JVM use java attach API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} | |
} |
tools.jar class path needed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
compile :
run :