How to Get a List of Current Open Processes Java?


It is essential for the user to know about the processes that are running in their system, be it foreground or background. To do so in windows we have a task manager but internally it uses a program called tasklist. Tasklist aside from giving us the currently running processes, it also gives us details like process id, session name, session and memory usage of each process.

In this article we will see how we can get a list of currently open processes by using Java programming language.

Algorithm

Step 1 − Create a process that executes the tasklist.exe

Step 2 − Create a BufferedReader class that takes in the process object.

Step 3 − Use a while loop to read the process details line by line and print it out.

Syntax

To run the tasklist.exe located in our system32 folder, we need to call a process obj and then execute.

Following is the syntax to execute the tasklist.exe program using the method:

Process process_object = Runtime.getRuntime().exec(System.getenv("windir") + "\system32" + "tasklist.exe");

NoteThis program will not work on any online editor. To get the list of current open processes in your local system you need to run it on your local editor (Java IDE) only.

Approach: List All Active Processes Using tasklist.exe

In this approach, we call a process that executes tasklist.exe in the WIN32 folder. After that we read all active processes using BufferedReader and print it to console one by one.

Example

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
   public static void main(String[] args) {
      try {

         // String variable to store process details
         String processes;

         // Execute tasklis.exe from win32
         Process p = Runtime.getRuntime().exec(System.getenv("windir") + "\system32" + "tasklist.exe");
         
         // Buffered reader to read from the process object
         BufferedReader br = new BufferedReader(newInputStreamReader(p.getInputStream()));
        
        // Prints all processes one by one
         while ((processes = br.readLine()) != null) {
            System.out.println(processes);
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

Image Name                    PID    Session Name     Session#    Mem Usage

========================= ======== ================ =========== ============
System Idle Process              0 Services                   0          8 K
System                           4 Services                   0      9,416 K
Registry                       140 Services                   0     30,420 K
smss.exe                       604 Services                   0      1,076 K
csrss.exe                      976 Services                   0      5,936 K
csrss.exe                     1112 Console                    1     14,144 K
winlogon.exe                  1164 Console                    1     11,704 K
wininit.exe                   1216 Services                   0      6,628 K
services.exe                 1260  Services                   0      9,804 K
lsass.exe                    1276  Services                   0     27,360 K
svchost.exe                  1396  Services                   0      1,388 K
fontdrvhost.exe              1428  Console                    1      6,608 K
...

In this article, we explored how to find the list of current open processes in Java.

Updated on: 04-Apr-2023

983 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements