Hi,
I'm writing a program and in this program I want to kill a running windows process. For this example lets say notpad.exe is open and I want to close this.
I have successfully started other applications with both ProcessBuilder and the older Runtime.getRuntime().exec().
Example code:
List<String> command = new ArrayList<String>();
command.add("cmd.exe");
command.add("/c");
command.add("tskill notepad");
ProcessBuilder builder = new ProcessBuilder(command);
Process p = builder.start();
stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = stdout.readLine()) != null) {
if (!line.trim().equals("")) {
log(line, StatusTextArea);
}
}
stdout.close();
stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = stderr.readLine()) != null) {
if (!line.trim().equals("")) {
log(line, StatusTextArea);
}
}
stderr.close();
exitValue = p.waitFor();
if (exitValue != 0) {
log("ERROR" + "tskill process failed, aborting, exit code: " + exitValue, StatusTextArea);
}
If I try to execute this I get: +'tskill.exe' is not recognized as an internal or external command, operable program or batch file.+
I have also tried without going through cmd and just executing "tskill notepad" but then I get:
Cannot run program "tskill notepad": CreateProcess error=2, The system cannot find the file specified
If I have understod everything correctly error=2 is that it cant find the application I want to execute. But I can execute both "cmd /C tskill notepad" and "tskill notepad" through start->run without any problem. I also tried to give the full path to tskill but that didnt work either.
Why cant my java app find tskill?
All other applications I have tried to start work just fine.