Killing the Service using Java; example code is below –
log.info("Forcefully Killing Services - Taskkill command length: "+arg.length);
String cmd = "cmd.exe /c taskkill /s "+
//e.g. taskkill /f /im notepad.exe or taskkill /PID 827
log.info("Command to Execute: "+cmd);
//execute the command
Process process = Runtime.getRuntime().exec(cmd);
If you want to get the process id of the service -
log.info("Getting PID - Tasklist command length: "+arg.length);
String cmd = "cmd.exe /c tasklist /s "+
//here service name can be prefix, like note*, it will list all the services which starts with note prefix.
log.info("Command to Execute: "+cmd);
Process process = Runtime.getRuntime().exec(cmd);
//to do a synchronous action till you get the result
int exitVal = process.waitFor();
//to check success or failure
log.info("Process exitValue: " + exitVal);
- You can take different actions on the services, and there are other task commands which can be automated using the above steps. However, its not much advisable to use Runtime.getRuntime().exec(