Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get PID w/o JNI

14 views
Skip to first unread message

Robert M. Gary

unread,
Mar 18, 2005, 4:20:25 PM3/18/05
to
Does anyone know of a way to get the JVM's process ID (Windows and UNIX) w/o
using JNI? Does 1.5 have an interface for this?

-Robert


Lisa

unread,
Mar 18, 2005, 4:34:16 PM3/18/05
to

"Robert M. Gary" <foo...@foobar.com> wrote in message
news:11111808...@cswreg.cos.agilent.com...

On windows XP the command is tasklist.


Gordon Beaton

unread,
Mar 19, 2005, 3:47:28 AM3/19/05
to
On Fri, 18 Mar 2005 13:20:25 -0800, Robert M. Gary wrote:
> Does anyone know of a way to get the JVM's process ID (Windows and
> UNIX) w/o using JNI? Does 1.5 have an interface for this?

Here are three general solutions for Unix, one specific for Linux:

- run your program from a shell script like this, then get the pid
from the property:

#!/bin/sh
exec java -Dpid=$$ MyApp

- run a child process that can tell you the pid of its parent:

String[] cmd = { "/bin/bash", "-c", "echo $PPID" };
Process p = Runtime.getRuntime().exec(cmd);

then read the pid from p's InputStream.

- run ps from Runtime.exec() and parse the output.

- on Linux you can get your pid from the process file system. Use a
FileReader to read /proc/self/stat. Other unices have similar
factilities, however many use a binary format that isn't so easily
parsed from Java.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Michiel Konstapel

unread,
Mar 21, 2005, 4:49:05 PM3/21/05
to
And the word of the day is

> factilities

fac·til·i·ty
n. pl. fac·til·i·ties

A utility that provides supposedly useful facts.


SCNR,
Michiel

0 new messages