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.

tucuxi
18k2 gold badges48 silver badges80 bronze badges
asked Sep 1, 2011 at 9:26

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);
answered Sep 1, 2011 at 9:36
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!

Matthias
3,5902 gold badges33 silver badges43 bronze badges
answered Oct 7, 2013 at 12:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.