views:

14

answers:

1

hello all,

I am looking for a way to check the state of a windows service through Java. From some basic search through Google and here it sounds like Java has no api to query the Windows Services.

On the Windows command prompt running: sc \some_host_name query "serviceName"

gets me the info i want. Now i want to be able to run that in a Java program and be able to parse the output.

Any one know of a way to run a Windows command through Java?

+1  A: 

Sounds like you need the Java 5+ ProcessBuilder.

A quick example (based the above documentation)

To start a process running:

Process p = new ProcessBuilder("sc", "\\some_host_name", "query", "serviceName").start();

The Process class provides methods to get the output (and error) stream from the process - it's standard stream handling from there.

The pre-Java 5 way of doing this was Runtime.exec(). I haven't actually used ProcessBuilder on Windows myself, drop a comment if you have problems?

Brabster