1

I'm using the following code in Java to kill two different Windows process by window title:

String cmd = "taskkill /F /FI \"WindowTitle eq " + windowTitle + "\" /T";

Process process = Runtime.getRuntime().exec(cmd); 

In the first case, system produces cmd command -> taskkill /F /FI "WindowTitle eq X*" /T and the process is killed successfully producing exit code of 0.

In the seconds case, system produces cmd command -> taskkill /F /FI "WindowTitle eq Y*" /T but the process remains alive and produces exit code of 255.

Both processes are produced by the same software vendor.

Why would one process be killed while the other one refuses to die?

Thanks

Bradford Griggs
  • 439
  • 2
  • 15
  • 1
    One possibility that I just checked: if one process is started as administrator then `taskkill` can only kill it if it is started as administrator too. The answer at https://stackoverflow.com/a/14596920/5646962 lists several possibilities how you can start `taskkill` as administrator. – Thomas Kläger Aug 23 '21 at 19:50
  • Any one of a number of reasons - no such window title, no access, etc.,etc. Needs more debugging info. What happens if you issue the 'taskkill' command manually? – user16632363 Aug 23 '21 at 19:51
  • @ThomasKläger Thanks for your response! I looked at the code used to launch both process, and you are correct, the second process was launched as Administrator. This is the same process that I cannot kill now. Tried updating the command to: `runas /profile /user:Administrator /savecred "cmd.exe taskkill /F /FI "WindowTitle eq Y*" /T"` as suggested on https://stackoverflow.com/a/14596649/16044298 but doesn't seem to work.. Tried both with and without `/savecred` – Bradford Griggs Aug 23 '21 at 20:33

1 Answers1

1

Just triggering the process as you do will very likely get stuck. This is not directly related to Java but how operating systems treat processes if their streams are not handled (see https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html).

So once you start a process, wait until it finishes. While waiting, ensure its stdout and stderr streams are cleared so the OS will not block the process.

While this answer does not tell you why sometimes processes get killed and sometimes not, I am sure once you try this there is enough information in your java output so you can figure it out.

The code could look something like this:

Process process = Runtime.getRuntime().exec(cmd);
InputStream pin = process.getInputStream();
InputStream perr = process.getErrorStream();
byte[] buffer = new byte[4096];
int read = 0;
while (process.isAlive()) {
    read = pin.read(buffer); // empty stdout so process can run
    if (read > 0) {
        System.out.write(buffer, 0, read);
    }

    read = perr.read(buffer); // empty stderr so process can run
    if (read > 0) {
        System.err.write(buffer, 0, read);
    }
}

// empty buffers one last time
read = pin.read(buffer);
while (read >= 0) {
    System.out.write(buffer, 0, read);
    read = pin.read(buffer);
}

read = perr.read(buffer);
while (read >= 0) {
    System.err.write(buffer, 0, read);
    read = perr.read(buffer);
}

// print process exit code
System.out.println("exited with "+process.getExitValue());
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Thanks for your response. Do you have an idea on how to achieve this via the command to: `runas /profile /user:Administrator /savecred "cmd.exe taskkill /F /FI "WindowTitle eq Y*" /T"` as suggested on https://stackoverflow.com/a/14596649/16044298? If you can provide insight on this I would upvote and accept it as the answer. – Bradford Griggs Aug 25 '21 at 01:39