start windows service from java

47,056

Solution 1

You can formulate a Command Prompt script to start, stop, and check status on a service using a String Array:

// start service
String[] script = {"cmd.exe", "/c", "sc", "start", SERVICE_NAME};

// stop service
String[] script = {"cmd.exe", "/c", "sc", "stop", SERVICE_NAME};

// check whether service is running or not
String[] script = {"cmd.exe", "/c", "sc", "query", APP_SERVICE_NAME, "|", "find", "/C", "\"RUNNING\""};

Execute scripts using the following:

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

Solution 2

import java.io.*;
import java.util.*;

public class ServiceStartStop {
    public static void main(String args[]) {
        String[] command = {"cmd.exe", "/c", "net", "start", "Mobility Client"};
        try {
            Process process = new ProcessBuilder(command).start();
            InputStream inputStream = process.getInputStream(); 
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
        } catch(Exception ex) {
            System.out.println("Exception : "+ex);
        }
    }
}

It worked fine .... instead of "sc" use "net" command.

Solution 3

You can execute system commands from java using the exec () command - A simple tutorial on the same can be found here - http://www.java-samples.com/showtutorial.php?tutorialid=8

Now you can use the system commands to start / stop windows services - A sample of the same can be found here

http://www.windowsitpro.com/article/registry2/how-can-i-stop-and-start-services-from-the-command-line-

I am not very sure about monitoring the status , so can't be of much help in that regards

Share:
47,056
user867662
Author by

user867662

Updated on July 09, 2022

Comments

  • user867662
    user867662 almost 2 years

    How can we start/stop a Windows Service from Java? For example, I would like to start and stop the mysql Windows Service from Java.

    If start/stop is possible, then is it possible to know whether the service is started successfully or not?

  • ecle
    ecle over 12 years
    sc.exe is more elaborate that net.exe and can help you to get more details about a Windows service. See sc /? for what it can do. However, both Windows' default utilities still need administrator privilege.
  • Emmanuel Touzery
    Emmanuel Touzery almost 8 years
    note that since java 1.7 you can just call inheritIO docs.oracle.com/javase/7/docs/api/java/lang/… to redirect to stdout the output from running a process.
  • Peter Kahn
    Peter Kahn about 7 years
    How do you handle need for elevation?
  • Lalit Bhudiya
    Lalit Bhudiya over 6 years
    If I try to start/Stop service without Admin rights, It gives Access Denied. Any Idea how to execute this with Admin access in JAVA ?
  • pavan
    pavan over 6 years
    One way I know is admin giving the rights to user with which you are trying to alter the services. I'm not a Windows user so can't explain in detail.
  • Tatkal
    Tatkal almost 6 years
    I tried using your example. It didn't work neither any exception was thrown. When I use sc instead of net, this error was displayed on console "[SC] StartService: OpenService FAILED 5: Access is denied."
  • lenikhilsingh
    lenikhilsingh over 4 years
    I'm getting this error: [SC] StartService: OpenService FAILED 5: Access is denied.
  • Pramod
    Pramod over 4 years
    May be little late, but for new users if you are getting access denied you have to execute the jar in a command prompt with admin rights or start eclipse or any other IDE as admin.
  • Hüseyin
    Hüseyin over 3 years
    What do you mean with SERVICE_NAME and APP_SERVICE_NAME ? Could you give an example about that?