Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java API to check status of Windows NT service?

Reply
Thread Tools

Java API to check status of Windows NT service?

 
 
The House Dawg
Guest
Posts: n/a
 
      12-01-2006
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

 
Reply With Quote
 
 
 
 
The House Dawg
Guest
Posts: n/a
 
      12-05-2006

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 );

}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MOSS 2007 API to implement check in & check out Sadhna ASP .Net 0 09-01-2008 11:57 AM
windows-api-0.2.0/lib/windows/api.rb:211: [BUG] Segmentation fault aidy.lewis@googlemail.com Ruby 2 10-25-2007 04:27 AM
.Net Profiler API in 64 bit windows -FunctionMapper callback API =?Utf-8?B?TGVv?= Windows 64bit 0 09-05-2007 06:10 PM
Recommend a free 'Status Monitor' or 'Dashboard' to report overnight tasks status? Shug Java 13 12-15-2006 06:22 PM
POST problem - IIS sc-win32-status:64; sc-status:400 saha ASP .Net 0 07-14-2005 07:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57