It seems that your are looking for a Linux centric solution rather than a portable one. On Linux the easy, efficient and reliable way to get the information you need from Java is to use the
proc filesystem, and especially the
/proc/self directory. The
man 5 proc manpage describe each file.
pid is first field of
/proc/self/stat. The image name is the second one.
It is very easy to write a class to wrap these calls. It will be only a few lines of code doing basic IO: fast and robust. Using third party programs is harder to get right and less efficient.
This solution is not portable since most of the Unix systems do not have a
proc filesystem. You have four options from the best to the worst, IMHO:
- Capture the information you need before starting the process: Could be easy or not depending of your application
- Find clean & system specific implementations like the
proc file system for Linux
- Use unspecified API.
RuntimeMXBean.getName() usually returns the
pid, you can also use reflection to get access to
java.lang.UnixProcess.pid. Implementation can differs between JVM and can change at any time. Acceptable solutions if you control the environment.
- Write native code to access the POSIX API
- Use external tools like
If you decide to rely on external tools then you should be very careful about portability (options and output can differs greatly between two OS) and locale settings.