Angsuman's Java Blog

News and views from my perspective as a Chief Software Architect and CEO. Focused on Java and Web Technologies.


Monday, January 20, 2003

How to get PID in Java



-----Original Message-----
From:
Sent: Sunday, January 19, 2003 11:32 AM
To: caljug@yahoogroups.com
Subject: RE: [caljug] Getting Process IDS from a Java Application

[snip]
I was just wondering if it was possible to get the pid's of all the running processes(something like ps
command in UNIX) from a Java application.
[snip]
--Reply--
Umm.. PID's on MAC? No sure if they have them. PID is very ?n[i,u]x specific. Even though windows have them, it is not very widely used or advertised.
In any case you can always get a platform specific solutioon by exec'ing the corresponding "ps" or executing the API through JNI.
However if you are only interested in PID's of applications exec'ed by the Java app. then you use hashCode of the Process as PID, and kill the process based on PID by fetching the corresponding Process & destroy() on it. This is cross-platform. The PID's used here as you can see are unique only within the JVM. BTW: PID here is defined as the unique identifier for a process within a JVM, this is not the same as the PID generated by the OS. However they will still have a 1-1 relationship.