Does anyone know how to get a list of all running processes on Linux and Solaris with Java? I don't want to use external programs like ps and I don't want to use proc. I want to do it with native calls. I found a way for Windows using JNA. But that way doesn't work for Linux and Solaris.
-
1For cross-reference, how to do it using `ps` would be here: http://stackoverflow.com/questions/54686/how-to-get-a-list-of-current-open-windows-process-with-java – Volker Stolz Nov 22 '12 at 07:43
-
There is no way to get that list without either running ps or using /proc (which is what ps does anyway). Your requirements cannot be met. – jlliagre Nov 23 '12 at 16:03
6 Answers
Java does not provide an api for this.
What you can do is parse the result of Process p = Runtime.getRuntime().exec("ps");

- 6,870
- 5
- 36
- 71
-
Thank you, but as I said above, "I don't want to use external programs like ps and I don't want to use proc." I'm searching for a solution with JNI / JNA. – Roland Nov 22 '12 at 18:37
Use the /proc
filesystem. This is how ps does it and I cannot find a reason why you shouldn't do it the same way.

- 8,023
- 5
- 37
- 52
Both Linux ps
and Solaris ps
eventually use the /proc implementations of their respective OSes. You cannot but use /proc, whether directly or indirectly.
Of course you could always look at the implementation of ps
and then do the same in JNI
- but you'd have to do it separately for every platform.

- 7,274
- 1
- 32
- 50
Use ps. If you don't want to tell the system to run the ps command, but instead go through JNI, then get a copy of the ps source code for Linux and OpenSolaris. Then write Java code that uses JNI to do the system calls used by the ps source code. Note that you will need to consider the low level details of the different OSes because they are not the same.
If you don't like that idea, then perhaps it is wisest to go back to the portable API represented by the ps command.

- 31,973
- 6
- 70
- 106
Have a look at Sigar:
http://support.hyperic.com/display/SIGAR/Home
With Sigar you can do what you are looking for (and much more) and it has JNI bindings.

- 8,591
- 1
- 17
- 27