'Howto get list of running processes in Java using JNA in a cross-platform environment?

I need to monitor servers in a cross-platform environment : Windows, Unix (Aix, Solaris) and Linux. It means : get processes, find some files, ...

So i am focusing on JNA (Java Native Access) in order to deal with this. With the hope of being able to have one code to "rule them all".

Please, note that i am not a C developer.

So i don't know "C standard Library" or POSIX library for instance.

My questions :

how could i discover list of running processes in a cross-platform way ? Is JNA likely to do this ? Which function(s) to invoke, from which library (if not C standard library) ?

Thanks a lot for your advises and feedback.



Solution 1:[1]

The other post linked in the comments lists the command line ps (for *nix) and taskinfo (for Windows) as the easiest approach. It doesn't use JNA but it gets you the answer reasonably reliably with little fuss.

I am the lead developer of the OSHI project which does exactly what you ask for Windows and macOS using JNA, and Linux using plain Java (one JNA function to follow a dynamic link), and four Unix flavors (AIX, Solaris, FreeBSD and OpenBSD) using the procfs. Source code is on that site for you to peruse at your leisure, or you can simply use that project as a dependency; you can find the getProcesses() methods in the platform-specific implementations of the OperatingSystem interface.

The short answer is that while you can use JNA it's not mandatory. In OSHI I use JNA's Platform class to determine which system you're running on (although even that is simply parsing the results of System.getProperty("os.name")) and based on which platform you are on, you branch off into platform-specific code.

For Linux, you simply read the /proc directory; every numeric file in that directory represents a process and has stats in the directory tree. This doesn't use JNA, but fit's much faster than trying to parse the C source code of the ps command which essentially leads to the same underlying information.

For Unix, parsing ps is an option. The -o flag lets you specify the fields you want to include.

For Windows you can use the Windows Process and Thread Functions (most of which are implemented in JNA already) or using the PSAPI functions via JNA as shown here, but the information returned is limited and requires a lot of jumping through hoops to get at details, some of which require elevated permissions. It's possible to query WMI via a COM interface in JNA to get all the info in one query. OSHI uses the registry to get most information from the same location WMI eventually would call.

For macOS X you use the proc_listpids() function to get the process IDs and feed that to proc_pidinfo(). These are in the System framework so JNA is used to implement the C calls in Java.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1