22

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user867662
  • 1,091
  • 4
  • 20
  • 45
  • 2
    In Windows, you can use sc.exe command from DOS command interpreter to start/stop/query a service name; for example: `sc \\10.8.0.1 query spserv` to query the current state of the service named "spserv" at PC with IP address 10.8.0.1. You can do this locally or remotely. – ee. Jan 31 '12 at 06:02
  • 1
    You can also use "net start/stop " to start the service. – Manish Jan 31 '12 at 06:04
  • for more information of service controller(sc.exe) check [service controller](http://ss64.com/nt/sc.html).And check the below enhanced answer.. – pavan Feb 07 '12 at 05:26

5 Answers5

25

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);
Alan
  • 506
  • 7
  • 24
pavan
  • 3,225
  • 3
  • 25
  • 29
  • 3
    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 ? – Lalit Bhudiya Aug 15 '17 at 15:23
  • 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. – pavan Sep 25 '17 at 06:16
  • I'm getting this error: `[SC] StartService: OpenService FAILED 5: Access is denied.` – lenikhilsingh Aug 13 '19 at 16:43
  • 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. – Pramod Dec 05 '19 at 11:06
  • What do you mean with SERVICE_NAME and APP_SERVICE_NAME ? Could you give an example about that? – Hüseyin Jan 20 '21 at 11:43
4
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.

PageNotFound
  • 2,320
  • 2
  • 23
  • 34
  • note that since java 1.7 you can just call `inheritIO` https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO() to redirect to stdout the output from running a process. – Emmanuel Touzery Jun 14 '16 at 07:22
  • 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." – Tatkal Jun 18 '18 at 10:18
2

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

Rocky
  • 941
  • 7
  • 11
  • `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. – ecle Jan 31 '12 at 14:37
  • How do you handle need for elevation? – Peter Kahn Feb 06 '17 at 16:43
0

You may execute following commands via Runtime#exec method.

net start and net stop (full information is available at : http://technet.microsoft.com/en-us/library/cc736564%28WS.10%29.aspx)

Probably you will have to use cmd /c net start as it would execute the command under shell.

jatanp
  • 3,982
  • 4
  • 40
  • 46
0

Did you try JNA? It has some good api to do this.

JNA is very active in SO - you could come back here with specific questions.

Community
  • 1
  • 1
Jayan
  • 18,003
  • 15
  • 89
  • 143