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