开发者

Executing command with runtime().exec

开发者 https://www.devze.com 2023-04-01 00:55 出处:网络
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\\\"\");

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!

0

精彩评论

暂无评论...
验证码 换一张
取 消