aspose file tools*
The moose likes Java in General and the fly likes Controlling Windows Services from a Java App Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Controlling Windows Services from a Java App" Watch "Controlling Windows Services from a Java App" New topic
Author

Controlling Windows Services from a Java App

John Davis
Ranch Hand

Joined: Nov 02, 2000
Posts: 189
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?

David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Have you tried net stop <serviceName>, i.e.

net stop "Crystal Cache Server"

?


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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".
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
Also you could use the sc command:
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
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

Joined: Nov 02, 2000
Posts: 189
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

Joined: Mar 03, 2004
Posts: 101
> 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

Joined: Nov 02, 2000
Posts: 189
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

Joined: Mar 03, 2004
Posts: 101
> 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Controlling Windows Services from a Java App