Executing command with runtime().exec
I try to check if a service is installed on Windows using:
Process p = Runtime.getRuntime().exec(
"sc query type= service state= all | find\"postgresq开发者_StackOverflow社区l\"");
but the output is exactly as if I had executed the sc
command by itself (a help message). When executing the same string via cmd
it works correctly.
Try like this:
String[] cmd = new String[4];
cmd[0] = "sc";
cmd[1] = "query";
cmd[2] ="type=service";
cmd[3] = "state= all | find\"postgresql\"";
Process p = Runtime.getRuntime().exec(cmd);
I have the same problem and tried the solution using an array, but it did not work for me.
So I used the command below. In my case I had to search the service in the return, because I had not the exact service name. In your case, you can put the process name or server name in the WHERE clause:
Process process = Runtime.getRuntime().exec("wmic SERVICE WHERE State=\"Running\" get Name,PathName /format:LIST");
Important: The wmic is present just in Windows XP Professional or higher!
精彩评论