I am developing a java program on the UNIX environment. I want to
get the id of my process. What should I do ? or what class method do
I use ?
Can you help me ?
There is no pure Java way. You have a number of platform dependent
alternatives however:
- call getpid() from a native method
- tell the application what its pid will be, by passing $$ either as a
property or an argument from a shell script:
#!/bin/sh
exec java -Dpid=$$ MyApp
- exec a child process that can tell you its parent pid:
String[] cmd = { "/bin/bash", "-c", "echo $PPID" };
Process p = Runtime.getRuntime().exec(cmd);
then read the pid from p's InputStream.
- on some platforms you can use a FileInputStream to read from the
process file system. For example on linux you can read the pid from
/proc/self/stat.
/gordon