how to find list of running processes in java

when we have to stop a process dynamically we must know about which processes are currently running on the system,This java program will lists all the currently running processes on windows using the tasklist.exe it will list all the exe applications currently running on the system.

package com.il.jdbc.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ListCurrentRunningProcesses {

	public static void main(String[] args) {

		List<String> currentprocessesList = new ArrayList<String>();
		try {
			String str;
			Process process = Runtime.getRuntime().exec("tasklist.exe /nh");
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(process.getInputStream()));
			while ((str = bufferedReader.readLine()) != null) {
				if (!str.trim().equals("")) {
					currentprocessesList
							.add(str.substring(0, str.indexOf(" ")));
				}
			}
			System.out.println("No. Of Processes Currently running : "
					+ currentprocessesList.size());
			System.out.println("Names of Running Processes :"
					+ currentprocessesList);
			bufferedReader.close();
		} catch (Exception err) {
			err.getMessage();
		}

	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *