Java Virtual machine Attach API - loadAgent after checking is it already loaded

123 Views Asked by At

Here the sample code

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());
        }
        }
  }
}

How to check VirtualMachine vm0 that whether the agent is loaded or not? So that I wont load it multiple times.

vm0.getSystemProperties();
vm0.getAgentProperties();

None of the gave any information on loaded agents

0

There are 0 best solutions below