0

I try to check if a service is installed on Windows using:

Process p = Runtime.getRuntime().exec(
   "sc query type= service state= all | find\"postgresql\"");

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.

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.