0

I'm accessing to running exe using below method. But I want to get the count of running exe files. Basically I want to get the count of exe process currently runs. Other than assigning all the process name to a list is there a simple way to obtain the count.

public boolean isProcessRunning(String serviceName) {

    try {
        Process pro = Runtime.getRuntime().exec(TASKLIST);
        BufferedReader reader = new BufferedReader(new InputStreamReader(pro.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            // System.out.println(line);
            if (line.startsWith(serviceName)) {
                return true;
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}
Don Nalaka
  • 129
  • 1
  • 11
  • And what's your problem? What do you see, what do you expect to see, ...? What is `TASKLIST`? ... – Dominique Nov 15 '20 at 09:37
  • *what's your problem? I want to get the count of exe process currently runs. Other than assigning all the process name to a list is there a simple way to obtain the count. *What do you see, what do you expect to see, ...? I don't know what to use. So currently stuck. What is TASKLIST? Sorry it is assigned to "tasklist" – Don Nalaka Nov 15 '20 at 10:01
  • 1
    Did you try `ProcessHandle.allProcesses().count()` ? See https://www.tutorialspoint.com/how-to-retrieve-all-processes-data-of-process-api-in-java-9, https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessHandle.html and https://stackoverflow.com/a/45068036/13211030 – Fullslack Nov 15 '20 at 10:32

0 Answers0