6

I have this so far:

public static void main(String[] args) {

    try {
        String line;
        Process p = Runtime.getRuntime().exec(
                System.getenv("windir") + "\\system32\\" + "tasklist.exe");

        BufferedReader input = new BufferedReader(new InputStreamReader(
                p.getInputStream()));

        while ((line = input.readLine()) != null) {
            System.out.println(line); // <-- Parse data here.
        }
        input.close();
    } catch (Exception err) {
        err.printStackTrace();
    }

    Scanner killer = new Scanner(System.in);

    int tokill;

    System.out.println("Enter PID to be killed: ");

    tokill = killer.nextInt();

}

}

I want to be able to kill a process based on the PID a user enters. How can I do this? (Only needs to work on Windows). *NB: Must be able to kill any process, inc. SYSTEM processes, so I'm guessing a -F flag will be needed if using taskkill.exe to do this?

So if I had

Runtime.getRuntime().exec("taskkill /F /PID 827");

how can I replace "827" with my tokill variable?

Raedwald
  • 40,290
  • 35
  • 127
  • 207
user1221937
  • 423
  • 2
  • 6
  • 7
  • 2
    This has been addressed [a](http://stackoverflow.com/questions/4633678/how-to-kill-a-process-in-java-given-a-specific-pid) [few](http://stackoverflow.com/questions/6356340/killing-a-process-using-java) [times](http://stackoverflow.com/questions/81902/how-to-find-and-kill-running-win-processes-from-within-java). Is there something specific about your circumstances? – Eric Grunzke Mar 05 '12 at 20:53
  • @EricGrunzke lucky link click? n1 :) – Aquarius Power Feb 02 '15 at 20:26

4 Answers4

7

Simply build the string to kill the process:

String cmd = "taskkill /F /PID " + tokill;
Runtime.getRuntime().exec(cmd);
Martijn Courteaux
  • 63,780
  • 43
  • 187
  • 279
2

I don't sit in front of a Windows computer right now. But if tasklist works for you, you can use ProcessBuilder in order to run the windows command taskkill. Call taskkill like this with a ProcessBuilder instance cmd /c taskkill /pid %pid% (replace %pid% with the actual pid). You don't need the absolute path to both executables because c:/windows/system32 is in the path variable.

As Eric (in a comment to your question) pointed out there are many who had this answer before.

alexvetter
  • 1,950
  • 2
  • 15
  • 41
1
String cmd = "taskkill /F /T /PID " + tokill;
Runtime.getRuntime().exec(cmd);

Use taskkill if you are on Windows.

You may want to use the /T option to kill all spawned child processes.

Ryuu
  • 374
  • 2
  • 4
  • 10
0

The JavaSysMon library does the trick and has the benefit of being multi-platform: https://github.com/danielflower/javasysmon (fork of the original, this one has a handy maven artifact)

private static final JavaSysMon SYS_MON = new JavaSysMon();

// There is no way to transform a [Process] instance to a PID in Java 8. 
// The sysmon library does let you iterate over the process table.
// Make the filter match some identifiable part of your process and it should be a good workaround
int pid = Arrays.stream(SYS_MON.processTable())
    .filter(p -> p.getName().contains("python"))
    .findFirst().get().getPid()

// Kill the process
SYS_MON.killProcess(pid);
// Kill the process and its children, or only the children
SYS_MON.killProcessTree(pid, descendentsOnly);
Col-E
  • 71
  • 1
  • 6