Start and Stop a Windows Service From Java

Is there any way to start and stop a Windows service from Java? The only post I found on it (http://forum.java.sun.com/thread.jspa?threadID=647509) had a link to one of the many aps that allow Java programs tobe services, which is not what I am interested in doing.

I am attempting to get data from performance counters from the Windows Performance Monitor into a Java ap without using JNI. I can get the data from C++ or a .net language pretty easily and was going to create a service that would listen for socket requests and feed back the data. However, I'd like to start and stop that service when my java code starts and stops. Is this possible? Would it make more sense to just use a .exe and Runtime.exec()?

[737 byte] By [ninthgirla] at [2007-10-3 1:20:32]
# 1
> Would it make more sense to just> use a .exe and Runtime.exec()?Yes.
cotton.ma at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...
# 2
... and this completely platform-specific app or piece of the app (to run on Windows only, and to control other Windows services) is appropriate for Java how?Wrong tool for the job.
warnerjaa at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...
# 3

> ... and this completely platform-specific app or

> piece of the app (to run on Windows only, and to

> control other Windows services) is appropriate for

> Java how?

>

> Wrong tool for the job.

It is a piece of an ap that might get the same performance information from several different platforms, depending that is a Swing UI. I'm not quite sure if the question of how to get the Windows specific piece is a "wrong tool" kind fo question. I am aware that Java is designed to be very portable. But at some point in time to get performance data you have to either use JNI or find another way to get the data in...from native code.

ninthgirla at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...
# 4

If it's only to start or stop a service then you could use the net command without any need for JNI.import java.io.*;

public class MsWinSvc {

static final String CMD_START = "cmd /c net start \"";

static final String CMD_STOP = "cmd /c net stop \"";

public static int startService(String serviceName) throws Exception {

return execCmd(CMD_START + serviceName + "\"");

}

public static int stopService(String serviceName) throws Exception {

return execCmd(CMD_STOP + serviceName + "\"");

}

static int execCmd(String cmdLine) throws Exception {

Process process = Runtime.getRuntime().exec(cmdLine);

StreamPumper outPumper = new StreamPumper(process.getInputStream(), System.out);

StreamPumper errPumper = new StreamPumper(process.getErrorStream(), System.err);

outPumper.start();

errPumper.start();

process.waitFor();

outPumper.join();

errPumper.join();

return process.exitValue();

}

static class StreamPumper extends Thread {

private InputStream is;

private PrintStream os;

public StreamPumper(InputStream is, PrintStream os) {

this.is = is;

this.os = os;

}

public void run() {

try {

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String line;

while ((line = br.readLine()) != null)

os.println(line);

}

catch (Exception e) {

e.printStackTrace();

}

}

}

}

Regards

jfbrierea at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...
# 5

> ... and this completely platform-specific app or

> piece of the app (to run on Windows only, and to

> control other Windows services) is appropriate for

> Java how?

>

> Wrong tool for the job.

Even though Java *is* platform independent doesn't mean that you are absolutely forbidden to use it for platform dependent purposes. Although I would object to anyone using it in such a way in a commercial application (or in an application intended for others, for that matter), I couldn't care less when someone does that to implement some little helper tool intended for his/her machine only.

My point is just that there's no absolute law that says: "platform dependent application => don't use Java". It might not have been your intention to imply that, but that's what I got from it.

Although I agree that the OP's using the wrong tool in this particular case, I do so for different reasons. There is a lot of support for controling Windows services in e.g. C#, which makes it a much more obvious choice to me.

dwga at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...
# 6

> Although I agree that the OP's using the wrong tool

> in this particular case, I do so for different

> reasons. There is a lot of support for controling

> Windows services in e.g. C#, which makes it a much

> more obvious choice to me.

I don't think I'm making myself terribly clear - somehow the Java code would need to control when a process - which would ideally be run in the background - started and stopped because that process needs to start and stop with the Java ap. Now, if there is another good way to get this process to run in the background from Java besides a service (there probably is, I haven't dug around too extensively) - that doesn't use ProcessBuilder, by the way, because that is not an option - I'm very open to hearing about it.

Thank you to the person who posted the sample code, by the way. That helps me with my decision.

ninthgirla at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...
# 7
http://javaservice.objectweb.org/this allows a java program to become a service. then you could start and stop with the code above i'd assume.Now can somone help me with my easy problem? http://forum.java.sun.com/thread.jspa?threadID=755312&tstart=0
JavaIsCoola at 2007-7-14 18:17:43 > top of Java-index,Java Essentials,Java Programming...