Share this page 

Detect if a process is running using WMIC Tag(s): Misc Prog HowTo Java Environment


WMIC is a powerful Windows utility. You can use to know if a particular process is running or not.

This example detects if a Tomcat instance (can be anything, ex: Apache or Excel) is running from a batch file.

@echo off
wmic process list brief | find /i "tomcat.exe"
set result=%ERRORLEVEL%
if "%result%"=="1" echo "not running"
if "%result%"=="0" echo "running"
/i is to make the find operation case-insensitive.

For Java code :

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

public class WindowsUtils {
  private WindowsUtils() {}
  public static boolean isProcessRunning(String processName) throws IOException {
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;

    List<String> command = new ArrayList<String>();
    command.add("WMIC");
    command.add("process");
    command.add("list");
    command.add("brief");

    try {
      ProcessBuilder builder = new ProcessBuilder(command);
      Process process = builder.start();
      is = process.getInputStream();
      isr = new InputStreamReader(is);
      br = new BufferedReader(isr);
      String line;
      processName = processName.toUpperCase();
      while ((line = br.readLine()) != null) {
        if (line.toUpperCase().indexOf(processName) > -1) return true;
      }
      return false;
    }
    finally {
      if (br != null) br.close();
      if (isr != null) isr.close();
      if (is != null) is.close();
    }
  }

  public static void main(String[] args) throws IOException {
    System.out.println(WindowsUtils2.isProcessRunning("excel.exe"));
  }
}
See also this HowTo.