kill Windows Process - Java Native OS

Java examples for Native OS:Windows

Description

kill Windows Process

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] argv) throws Exception {
        String processName = "java2s.com";
        killWindowsProcess(processName);
    }/*  w  ww . ja  v a  2  s .  co m*/

    public static void killWindowsProcess(String processName)
            throws Exception {
        if (isRunning(processName)) {
            getRuntime().exec("taskkill /F /IM " + processName);
        }

    }

    private static boolean isRunning(String processName) throws Exception {
        Process listTasksProcess = getRuntime().exec("tasklist");
        BufferedReader tasksListReader = new BufferedReader(
                new InputStreamReader(listTasksProcess.getInputStream()));

        String tasksLine;

        while ((tasksLine = tasksListReader.readLine()) != null) {
            if (tasksLine.contains(processName)) {
                return true;
            }
        }

        return false;
    }

    private static Runtime getRuntime() {
        return Runtime.getRuntime();
    }
}

Related Tutorials