有没有办法使用java获取/挂钩/附加已经运行的进程?

我希望能够做到这样的事情:

Process p = getRunningProcess(pid) 

如果有办法,那么创建进程的方式是否重要(使用java,使用python,从shell等等)?

可以从Java应用程序连接到另一个JVM进程(例​​如,能够to monitor what's going on and potentially detect problems before they happen )。 您可以使用Attach API执行此操作。 不太了解附加到非JVM进程。

 String name = ... List vms = VirtualMachine.list(); for (VirtualMachineDescriptor vmd: vms) { if (vmd.displayName().equals(name)) { VirtualMachine vm = VirtualMachine.attach(vmd.id()); String agent = ... vm.loadAgent(agent); // ... } }