Java Utililty Methods Kill Process

List of utility methods to do Kill Process

Description

The list of methods to do Kill Process are organized into topic(s).

Method

voidkillProcess(int port)
kill Process
int pid = getPid(port);
if (pid == 0) {
    return;
String[] command = { "taskkill", "/F", "/T", "/PID", Integer.toString(pid) };
if (System.getProperty("os.name").startsWith("Linux")) {
    String[] cmd = { "kill", "-9", Integer.toString(pid) };
    command = cmd;
...
voidkillProcess(String name)
kill Process
try {
    ProcessBuilder processBuilder = new ProcessBuilder("taskkill ", "/IM", name);
    Process process = processBuilder.start();
    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = null;
    try {
        while ((line = br.readLine()) != null) {
    } catch (Exception e) {
        e.printStackTrace();
        if (br != null) {
            br.close();
} catch (Exception e) {
voidkillProcess(String processName)
kill Process
String command = ("killall " + processName);
try {
    Process p = Runtime.getRuntime().exec(command.split(" "));
    p.waitFor();
} catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
...
booleankillProcess(String processName)
Kills the Windows process name
if (isProcessRunning(processName)) {
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("taskkill", "/F", "/IM", processName);
    Process process = processBuilder.start();
    process.waitFor();
    return !isProcessRunning(processName);
return false;
...
voidkillProcessesTree(String rootNamePart)
kill Processes Tree
List<String> chrome;
try {
    chrome = getPIDsByNamePart(rootNamePart);
    for (String s : chrome) {
        int pid = Integer.parseInt(s);
        killChildProcesses(pid);
    killProcessesByNamePart(rootNamePart);
...
booleankillProcessNix(String pid)
kill Process Nix
Runtime.getRuntime().exec("kill " + pid).waitFor();
return true;
voidkillProcessWithArgs(List args)
kill Process With Args
List<String> list = new ArrayList<>();
list.add("/usr/bin/pkill");
list.addAll(args);
try {
    Process child = new ProcessBuilder(list.toArray(new String[list.size()])).start();
    child.waitFor();
} catch (InterruptedException | IOException e) {
    throw new RuntimeException("Can't kill process: " + e.getMessage());
...