Java examples for java.lang:Process
Kills the Windows process name
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.util.Scanner; public class Main { public static void main(String[] argv) throws Exception { String processName = "java2s.com"; System.out.println(killProcess(processName)); }//from w w w.j av a2 s.c om /** * Kills the Windows process name * * @param processName The process to kill * @return True if it was successful, false otherwise */ static boolean killProcess(String processName) throws Exception { if (isProcessRunning(processName)) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("taskkill", "/F", "/IM", processName); Process process = processBuilder.start(); process.waitFor(); return !isProcessRunning(processName); } return false; } public static boolean isProcessRunning(String processName) throws IOException { ProcessBuilder processBuilder = new ProcessBuilder("tasklist"); Process process = processBuilder.start(); String tasksList = toString(process.getInputStream()); return tasksList.contains(processName); } private static String toString(InputStream inputStream) { Scanner scanner = new Scanner(inputStream, "UTF-8") .useDelimiter("\\A"); String string = scanner.hasNext() ? scanner.next() : ""; scanner.close(); return string; } }