List currently running processes (Windows)Tag(s): Environment
Using TASKLIST.EXE
The Microsoft TASKLIST.EXE is used to dump the list of the currently running processes. It is similar to tasklist window but for the console.From a Java program, we are launching TASKLIST.EXE and capture its output.
Note : TASKLIST.EXE is not included the HOME edition of XP. But you can download it from Web, for example : http://www.computerhope.com/download/winxp.htm.
import java.io.*; import java.util.*; public class WindowsUtils { public static List<String> listRunningProcesses() { List<String> processes = new ArrayList<String>(); try { String line; Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh"); BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream())); while ((line = input.readLine()) != null) { if (!line.trim().equals("")) { // keep only the process name line = line.substring(1); processes.add(line.substring(0, line.indexOf("""))); } } input.close(); } catch (Exception err) { err.printStackTrace(); } return processes; } public static void main(String[] args){ List<String> processes = listRunningProcesses(); String result = ""; // display the result Iterator<String> it = processes.iterator(); int i = 0; while (it.hasNext()) { result += it.next() +","; i++; if (i==10) { result += "\n"; i = 0; } } msgBox("Running processes : " + result); } public static void msgBox(String msg) { javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, msg, "WindowsUtils", javax.swing.JOptionPane.DEFAULT_OPTION); } }
Using a VBS
Another technique to build the required VBScript on-the-fly, execute it and capture its output.import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.InputStreamReader; import java.util.*; public class VBSUtils { private VBSUtils() { } public static List<String> listRunningProcesses() { List<String> processList = new ArrayList<String>(); try { File file = File.createTempFile("realhowto",".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file); String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n" + "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n" + "Set service = locator.ConnectServer()\n" + "Set processes = service.ExecQuery _\n" + " (\"select name from Win32_Process\")\n" + "For Each process in processes\n" + "wscript.echo process.Name \n" + "Next\n" + "Set WSHShell = Nothing\n"; fw.write(vbs); fw.close(); Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream())); String line; while ((line = input.readLine()) != null) { processList.add(line); } input.close(); } catch(Exception e){ e.printStackTrace(); } return processList; } public static void main(String[] args){ List<String> processes = VBSUtils.listRunningProcesses(); String result = ""; Iterator<String> it = processes.iterator(); int i = 0; while (it.hasNext()) { result += it.next() +","; i++; if (i==10) { result += "\n"; i = 0; } } msgBox("Running processes : " + result); } public static void msgBox(String msg) { javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null, msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION); } }
See this HowTo to check for a specific application is running or not.