Java API to check status of Windows NT service?

Discussion in 'Java' started by The House Dawg, Dec 1, 2006.

  1. Can anyone share a code snippet written in java that takes the name of
    a Windows NT service and returns the status of the service? Running,
    Paused, Stopped, etc.

    TIA,
    Matt
    The House Dawg, Dec 1, 2006
    #1
    1. Advertising

  2. The House Dawg wrote:
    > Can anyone share a code snippet written in java that takes the name of
    > a Windows NT service and returns the status of the service? Running,
    > Paused, Stopped, etc.
    >
    > TIA,
    > Matt


    Folks,

    There's probably a million ways to do this, but I found the following
    works well:

    bool isServiceStopped ( String serviceName ) throws Exception
    {
    boolean serviceStopped = false;

    /************************************************************
    * Construct the command, putting quotes around the service *
    * name in case the service name contains any blanks. *
    ************************************************************/
    String command = "sc query " + "\"" + serviceName + "\"";

    /*****************************************************************
    * Execute the command and get a buffered reader for the output. *
    *****************************************************************/
    Process proc = Runtime.getRuntime ( ).exec ( command );
    InputStream is = proc.getInputStream ( );
    BufferedReader br = new BufferedReader ( new InputStreamReader (
    is ) );
    String line = null;


    /*********************************************************************************
    * Read the command output until the STATE token is found or EOF is
    encountered. *

    *********************************************************************************/
    while ( ( line = br.readLine ( ) ) != null )
    {
    /********************************************
    * Check if the STATE token has been found. *
    ********************************************/
    if ( line.indexOf ( "STATE" ) > 0 )
    {
    /******************************************
    * Check if the service STATE is STOPPED. *
    ******************************************/
    if ( line.indexOf ( "STOPPED" ) > 0 )
    {
    serviceStopped = true;
    }
    break;
    }
    }
    proc.waitFor ( );

    /*****************************
    * Cleanup system resources. *
    *****************************/
    is.close ( );
    br.close ( );

    return ( serviceStopped );

    }
    The House Dawg, Dec 5, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. saha
    Replies:
    0
    Views:
    4,061
  2. Shug
    Replies:
    13
    Views:
    899
    Philipp
    Dec 15, 2006
  3. Shug
    Replies:
    13
    Views:
    954
    Philipp
    Dec 15, 2006
  4. Sadhna
    Replies:
    0
    Views:
    626
    Sadhna
    Sep 1, 2008
  5. Replies:
    2
    Views:
    143
    bbiker
    Oct 25, 2007
Loading...

Share This Page