Obtain the Pid
1 posts in topic
Flat View  Flat View

Posted By:   shweta_K
Posted On:   Monday, April 26, 2004 01:21 AM

How to get the process id(pid) of the java application
that is running, from within the same java code?

Re: Obtain the Pid

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, April 26, 2004 02:01 PM

I had to do a temporary hack once for some debugging. I think you have to step outside the Java worldview to get such non-portable info.

Maybe we didn't do it the best way but it was just a quick hack for debugging.... First we had to check if it was running under Windows or Unix. Then depending on that, we had to issue the appropriate command in a Runtime.exec() e.g. "ps" or whatever, Windows Resource Kit equivalent, I forget. Then we had to parse the output for something we could recognize (e.g. RTFM ps, or the Windows Resource Kit equivalent's doc).

If you're stuck on Solaris instead of Linux/MacOS/BSD etc., I would explicitly go with "/usr/ucb/ps" (the one that takes "aux" args) instead of just "ps", by the way... the SysV "ps" (the one that takes "elf" args) is not as useful, in my opinion, since it doesn't give you the "argv[0]" command name by which the process was actually invoked. With the real "ps" (Linux etc.) AKA "/usr/ucb/ps" (on Solaris), especially as "ps auxwww" to get full details in as wide an output format as possible, you do get this argv[0] for each process, which is easier to parse/search for.

About | Sitemap | Contact