Share this page 

Get a PID given a process nameTag(s): Environment


This solution is Windows only, we launch WMIC utility and capture its output to retrieve PIDs for given process name.
For a Java solution see below
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() {}

  // returns an ArrayList of the Pids given a processname (the executable name), empty if none
  public static ArrayList<Long> getPids(String processName) throws IOException {
    InputStream is = null;
    InputStreamReader isr = null;
    BufferedReader br = null;

    ArrayList<Long> pids = new ArrayList<Long>();

    List<String> commands = new ArrayList<String>();
    commands.add("WMIC");
    commands.add("process");
    commands.add("where");
    commands.add("name='"+ processName+"'");
    commands.add("get");
    commands.add("processid");

    String line;
    try {
      ProcessBuilder builder = new ProcessBuilder(commands);
      Process process = builder.start();
      is = process.getInputStream();
      isr = new InputStreamReader(is);
      br = new BufferedReader(isr);

      // first line is a header if we have a result
      line = br.readLine();
      if ("processid".equalsIgnoreCase(line.trim())) {
         System.out.println("*** " + line);
          // next line are the  PIDs
         while ((line = br.readLine()) != null) {
            if (line.trim().length() > 0) {
               pids.add(Long.parseLong(line.trim()));
            }
         }
      }
      else {
         System.out.println(line);
      }
      return pids ;
    }
    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(WindowsUtils.getPids("notepad++.exe"));
  }
}
With JDK9, the solution is not using an external utility and is more portable.
import java.io.IOException;
import java.lang.ProcessHandle.Info;
import java.util.ArrayList;
import java.util.stream.Stream;

public class WindowsUtils9 {
  private WindowsUtils9() {}

  // returns an ArrayList of the Pids given a processname, empty if none
  // JDK9
  public static ArrayList<Long> getPids(String processName) throws IOException {

     ArrayList<Long> pids = new ArrayList<Long>();
     Stream<ProcessHandle> processStream = ProcessHandle.allProcesses();

     Stream<ProcessHandle> toReturnProcessHandle = processStream.filter(processHandle -> {
        if(processHandle.isAlive()){
           Info info = processHandle.info();
           if(info.command().toString().toLowerCase().endsWith(processName.toLowerCase()+"]")){
              return true;
           }
        }
        return false;
     });
     toReturnProcessHandle.forEach(handle-> pids.add(handle.pid()));
     return pids;
  }

  public static void main(String[] args) throws IOException {
    System.out.println(WindowsUtils9.getPids("notepad++.exe"));
  }
}
If the process is started from a ProcessBuilder then you can get its PID easily :
import java.io.IOException;
public class ProcessBuilderGetPidDemoDemo
{
   public static void main(String[] args) throws IOException {
      Process p = new ProcessBuilder("notepad.exe").start();
      System.out.println(`The PID for notepad.exe is " + p.getPid());
   }
}
To kill a process using its PID, see Kill a process with a PID.