TMXStyles
TMXStyles

Reputation: 35

In java waiting for a windows service to start/stop?

I'm looking for a way to keep checking the status of a Windows Service in a loop until it has fully stooped or started.

I used Malik Ehtasham code Here to get the status of the service. But I have to keep pressing a button to get the status. I would like for it to just keep checking status saying... please wait.. then say stooped or started.

This is what i have to start the service:

String[] StartSpooler = {"cmd.exe", "/c", "sc", "start", "spooler"};
    try {
        Process process = new ProcessBuilder(StartSpooler).start();
        InputStream inputStream = process.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String StartSpoolerLine;
        while ((StartSpoolerLine= bufferedReader.readLine()) != null) {
            System.out.println(StartSpoolerLine);
        }
    } catch (Exception ex) {
        System.out.println("Exception : " + ex);
    }

This is what I have to keep checking the service status but its not working

    try {
        Process p = Runtime.getRuntime().exec("sc query spooler");

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = reader.readLine();
        while (line != null) {
            if (line.trim().startsWith("STATE")) {
                while (!"4".equals(line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim())) {
                   System.out.println("Starting....");    
                 }
System.out.println("Service is started and running");
            }
            line = reader.readLine();
        }

    } catch (IOException e1) {
    } 

Please help, thanks!

Upvotes: 2

Views: 3717

Answers (2)

user3420847
user3420847

Reputation: 220

This works great for me.

private static String[] scriptStart = { "net", "start", SERVICE_NAME};
  private static String[] scriptStop = {"net", "stop", SERVICE_NAME};


Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(scriptStart);//or scriptStop
    process.waitFor();

Upvotes: 2

user3880045
user3880045

Reputation:

I have used your code and this is working on my window machine..Please try again.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class WindowServiceTest {

    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("sc query spooler");

            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = reader.readLine();
            while (line != null) {
                if (line.trim().startsWith("STATE")) {
                    while (!"4".equals(line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim())) {
                       System.out.println("Starting....");    
                     }
                      System.out.println("... Service is started and running");
                }
                line = reader.readLine();
                System.out.println("SERVICE DETAILS --> "+ line);
            }

        } catch (IOException e1) {
        } 

    }

}


OUTPUT
===========
SERVICE DETAILS --> SERVICE_NAME: spooler
SERVICE DETAILS -->         TYPE               : 110  WIN32_OWN_PROCESS (interactive)
SERVICE DETAILS -->         STATE              : 4  RUNNING 
... Service is started and running
SERVICE DETAILS -->                                 (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
SERVICE DETAILS -->         WIN32_EXIT_CODE    : 0  (0x0)
SERVICE DETAILS -->         SERVICE_EXIT_CODE  : 0  (0x0)
SERVICE DETAILS -->         CHECKPOINT         : 0x0
SERVICE DETAILS -->         WAIT_HINT          : 0x0
SERVICE DETAILS --> null

Upvotes: 3

Related Questions