查看是否有在运行cmd.exe,代码如下
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { boolean isrun=isRunning("cmd.exe"); if(isrun){ System.out.println("有在运行cmd.exe"); }else{ System.out.println("没有运行cmd.exe"); } } public static boolean isRunning(String processName)//判断有没有在运行考试软件 { BufferedReader bufferedReader = null; try { Process proc = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq " + processName + "\""); bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while ((line = bufferedReader.readLine()) != null) { if (line.contains(processName)) //判断是否存在 { return true; } } return false; } catch (Exception ex) { ex.printStackTrace(); return false; } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (Exception ex) { } } } } }
关闭cmd.exe
Runtime.getRuntime().exec("taskkill /f /im cmd.exe");