有没有办法使用java获取/挂钩/附加已经运行的进程? [英] Is there a way to get/hook/attach an already running process using java?

查看:129
本文介绍了有没有办法使用java获取/挂钩/附加已经运行的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to do something like that:

Process p  = getRunningProcess(pid)

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

If there's a way, does it matter how the process was created (using java, using python, from the shell, etc...)?

推荐答案

可以从Java应用程序附加到另一个JVM进程(例​​如,能够监视正在发生的事情并在问题发生之前检测到问题)。您可以使用附加API 来执行此操作。不太了解附加到非JVM进程。

It is possible to attach to another JVM process from Java app (e.g. to be able to monitor what's going on and potentially detect problems before they happen). You can do this by using the Attach API. Don't know much about attaching to non-JVM processes.

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);
        // ...
    }
}

这篇关于有没有办法使用java获取/挂钩/附加已经运行的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆