Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Get a process given a pid

Tags:

java

process

pid

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

thanks

like image 719
Th3sandm4n Avatar asked Oct 21 '10 20:10

Th3sandm4n


1 Answers

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).

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.

like image 198
André Caron Avatar answered Sep 22 '22 01:09

André Caron