How to get process (external program) time, cpu usage for process run from java application?

StackOverflow https://stackoverflow.com/questions/17188093

  •  01-06-2022
  •  | 
  •  

Вопрос

I know how to run an external program in java:

public class Test {
  public static void main(String[] args) throws Exception {
    Process p = Runtime.getRuntime().exec(
       "\"c:/my-simple-app.exe\"");
    p.waitFor();
  }
}

But how can I get all of the program properties when I run it like this? I mean: system time for this process (how much system time it took to run), cpu usage (only for this exact process), ... Is it possible?

Это было полезно?

Решение

Run this program in a separate thread, then run tasklist /v process (if Windows), intercept output, split lines into columns, find my-simple-app.exe and get necessary info. If tasklist info is not enough, then read process ID column form tasklist output and run some other util to get more info.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top