• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Controlling Windows Services from a Java App

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm developing a Java Swing App to automate installation and configuration of some Off the shelf products. At a certain point in a product installation, it becomes imperative that I stop a Service (to avoid a file in use problem in another installation).
I am using runtime.exec (see code) to execute various windows installers etc.
Are there commands available which I could execute to exercise control on Windows services?

 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried net stop <serviceName>, i.e.

net stop "Crystal Cache Server"

?
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify...

"net" is a set of windows network commands:


C:\>net
The syntax of this command is:
NET [ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
HELPMSG | LOCALGROUP | NAME | PAUSE | PRINT | SEND | SESSION |
SHARE | START | STATISTICS | STOP | TIME | USE | USER | VIEW ]
C:\>



"net stop" is one of the subcommands (as above).


C:\>net stop
The syntax of this command is:
NET STOP service
C:\>



As you might guess, there is of course a parallel "net start".
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also you could use the sc command:
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
re: sc

Too bad it doesn't exist on my Win2K box:

C:\>sc
'sc' is not recognized as an internal or external command,
operable program or batch file.
 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,

Yes, I noticed that too when I tried it. I don't have an sc command on my Windows XP Home system either.

I am running into problems executing net stop "service name" from my runtime. The process never ends.

I have code to spawn threads to empty error and output streams from the net stop process. These threads never get end of file on their streams
and my main application never gets control back from





Do you have any hints as to what may be going wrong?

Here is the code which sets up and invokes net stop and checks for completion.



[ Jess adjusted the 2nd code block to shrink the whitespace so it wouldn't stretch the screen ]
[ September 23, 2004: Message edited by: Jessica Sant ]
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Too bad it doesn't exist on my Win2K box:

Yeah, sc.exe comes with the Windows 2000 Prof. Technical Resource Kit.
It also comes with Windows XP Pro.

> Do you have any hints as to what may be going wrong?

You should do:
 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jean-Francis,

First of all, thanks a lot for you help.

Actually, I found out what was wrong and everything is running OK now.

The problem is that I (and you apparently) assumed that net is internal to command.com or cmd.exe. The fact is is that it is not a command processor internal. It is some other kind of windows executable (.exe?) and therefore should be invoked in the exec as "net stop \"SERVICE NAME\" "
This works fine, command.com or cmd.exe are not involved.
I also discovered that the command "net stop 'SERVICE NAME' " does not work.
The runtime exec requires double quotes on a command line parameter which has embedded spaces, so the internal double quotes must be escaped.
 
Jean-Francois Briere
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> The problem is that I (and you apparently) assumed that net is internal to command.com or cmd.exe.

I didn't assume it. The point I wanted to make is that when using cmd.exe, you must use the "/c" flag. That's all.
For instance, don't do "cmd.exe set" but rather "cmd.exe /c set".

By the way it works perfectly well if you try it with a command-line executable, like net.exe, as I previously wrote.
But you're right, it's overhead to use the cmd shell for any command-line executable.

Cheers
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic