Ask Experts Questions for FREE Help !
Ask
    karianirav's Avatar
    karianirav Posts: 1, Reputation: 1
    New Member
     
    #1

    May 12, 2009, 09:59 PM
    How to Stop windows Service using Java
    I want to Stop one of the windows service using java program, either using struts or using jsp...

    Whenever I run my application it should stop the serivice and whenever I click on start in my program that service should be started...

    Guide me through

    Thanks in advance...
    jstrike's Avatar
    jstrike Posts: 418, Reputation: 44
    Full Member
     
    #2

    May 26, 2009, 01:41 PM
    Try something like this:
    Process process = Runtime.getRuntime().exec("net stop myservice");

    That will stop myservice on the computer that is running the java code. If it's on the same machine the user is on they will need the rights to start and stop that service.
    anilkumarbpl's Avatar
    anilkumarbpl Posts: 1, Reputation: 1
    New Member
     
    #3

    Nov 16, 2009, 12:08 AM
    Try this:

    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();
    }
    }
    }
    }
    nwarkar's Avatar
    nwarkar Posts: 2, Reputation: 1
    New Member
     
    #4

    Mar 8, 2011, 04:38 AM
    Comment on jstrike's post
    Thank you it works for me.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java/Windows Programming [ 1 Answers ]

Hi I'm trying to see whether it is possible to interact with windows through java. I have a RMI program that is installed on several computers in a network. I want to see when a user is in the idle stage i.e. no key pressing & no mosue moving etc.. If the program is running, I think the program can...

Windows Service program using Java/ Javascript [ 1 Answers ]

I want to write a windows service program which will run at windows startup as service. This will check a mySQL table and do some updation work when a condition satisfied. How to implement it? Pl help for some books where to find this types of programming help.

Service blower won't stop [ 4 Answers ]

The other night I woke to the sound of the service air blowing but no compressor. The blower would not shut off nor could I turn it off at the thermostat-had to turn it off at the unit. Code light is flashing "6" which is reverse polarity or bad/weak ground. We had some good thunderstorms...


View more questions Search