Monday, February 6, 2012

Kill a Windows process with Java


I would like to kill a process in Java for an application in Java 1.4. ; I don't want to (can't) add some custom libraries so I will use 'Runtime' class to execute a DOS command:
tasklist: allow me to recover all processes running.
taskkill: allow me to destroy a process by its ID.

This is a Java class which will use these both commands to do the job:

KillProcess.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class KillProcess {
      public static final int FIRST_COLUMN_LENGTH = 26;
      
      /**
      * pass all statements which must be found in the process description
      *     process definition columns are:
      *            - Image Name
      *            - PID
      *            - Session Name
      *            - Session#
      *            - Mem Usage
      *            - Status
      *            - User Name
      *            - CPU Time
      *            - Window Title
      * @param args {String[]}: statements which must be found in the process description
      * @throws IOException
      */
      public static void kill(String[] args) throws IOException{
            int processId = 0;
            // recover a file output from the DOS command 'tasklist'
            // which list all running processes
            Process process = Runtime.getRuntime().exec("tasklist /v");
            InputStream inputstream = process.getInputStream();
            InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
            BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
            String line;
            while ((line = bufferedreader.readLine()) != null) {
                  boolean blnFound = true;
                  // loop on all statements to verify if the line contains it
                  for (int i=0;ilength;i++){
                        if (line.indexOf(args[i])<0){
                              blnFound = false;
                        }
                  }
                  if (blnFound){
                        line = line.substring(FIRST_COLUMN_LENGTH);
                        while(line.charAt(0)==' '){
                              line=line.substring(1);
                        }
                        String strProcessId = line.substring(0, line.indexOf(' '));
                        processId = Integer.parseInt(strProcessId);
                  }
            }
            // verify the process has been found
            if (processId!=0){
                  // kill the process via 'taskkill'
                  Runtime.getRuntime().exec("taskkill /pid " + processId + " /f");
                  System.out.println("The process "+processId+" has been killed!");
            } else{
                  System.out.println("Process not found!");
            }
      }
      public static void main(String[] args) throws IOException{
            kill(args);
      }
}

This is an example of class use which will kill my IrfanView process ...

java KillProcess i_view32.exe IrfanView

No comments: