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