Java:获取给定pid的进程 [英] Java: Get a process given a pid

查看:683
本文介绍了Java:获取给定pid的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已知当前正在运行的进程,如何将其转换为Java中的Process对象?
进程已在运行,因此我不想生成另一个进程,我只是想将它封装到我可以在java代码中使用的Process对象中。
类似于:

Say I have a current running process known, how can I turn this into a Process object in Java? The process is already running, so I don't want to spawn off another one, I just want to encapsulate it into a Process object that I can use within the java code. Something along the lines of:

int pid = getPid();
Process proc = magicGetProcess(pid);

谢谢

推荐答案

我认为只使用内置库是不可能的。 AFAIK,让运行进程拥有自己的PID已经非常简单(参见功能请求) 备用机制 )。

I don't think this is possible using only the builtin library. AFAIK, it is already non-trivial to get the running process' own PID (see the feature request and alternate mechanisms).

快速浏览 java.lang.Process class 表明你可以编写 java.lang.Process 使用JNI和本机代码。然后,您的自定义类可以实现额外的方法,例如您问题中的方法。

A quick look at the java.lang.Process class shows that you could go about writing your custom implementation of java.lang.Process using JNI and native code. Your custom class could then implement extra methods, such as the one in your question.

这篇关于Java:获取给定pid的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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