Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Java API to check status of Windows NT service?

Thread view: 
The House Dawg - 01 Dec 2006 22:08 GMT
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 - 05 Dec 2006 21:43 GMT
> 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 );

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.