Skip to content
Advertisement

Java code – Best way to list java processes in localhost JVM (for Linux & Windows]

I’m writing a console app that will run in Linux and Windows systems. This app will mainly run on JRE 1.8 environments.

In my code, it needs to get a list of Java processes that run on the host running my application. I have read the posts about jps, which come as a part of JDK, but not JRE, and also it’s a separate executable.

My question is, is there any java library that I can use, to list (and ideally also kill some of) the running Java processes? Preferably, I would not want to hassle of calling jps executable with sth like Runtime.exec().

Thanks in advance, for any suggestions.

Advertisement

Answer

I finally found the 3rd party library named Oshi, which apparently does not need JDK and is compatible with Java 1.8: https://github.com/oshi/oshi

For future reference, here is a sample method:

public static List<OSProcess> getOSProcesses() {
    SystemInfo si = new SystemInfo();
    OperatingSystem os = si.getOperatingSystem();
    return os.getProcesses();
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement