www.pudn.com > TestToolsProject.rar > ProcessCloseProgram.java, change:2013-08-04,size:1243b


package com.mervyn.testsource; 
 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 
public class ProcessCloseProgram { 
 
 
	 public  void closeProcess(String programName){ 
	  Process listprocess; 
	  try { 
	   listprocess = Runtime.getRuntime().exec("cmd.exe /c tasklist"); 
	   InputStream is = listprocess.getInputStream(); 
	   BufferedReader r = new BufferedReader(new InputStreamReader(is)); 
	   String str = null; 
	   while ((str = r.readLine()) != null) { 
	    String id = null; 
	    Matcher matcher = Pattern.compile(programName + "[ ]*([0-9]*)").matcher(str); 
	    while (matcher.find()) { 
	     if (matcher.groupCount() >= 1) { 
	      id = matcher.group(1); 
	      if (id != null) { 
	       int pid = 0; 
	       try { 
	        pid = Integer.parseInt(id); 
	        } catch (NumberFormatException e) { 
	         e.printStackTrace(); 
	         } 
	        if (pid != 0) { 
	         Runtime.getRuntime().exec("cmd.exe /c taskkill /f /pid " + pid); 
	         } 
	        } 
	      } 
	    } 
	   } 
	  } catch (IOException e) { 
	   e.printStackTrace(); 
	  } 
 
	 } 
	}