start windows service from java
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
I am not very sure about monitoring the status , so can't be of much help in that regards
user867662
Updated on July 09, 2022Comments
-
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 over 12 years
sc.exe
is more elaborate thatnet.exe
and can help you to get more details about a Windows service. Seesc /?
for what it can do. However, both Windows' default utilities still need administrator privilege. -
Emmanuel Touzery almost 8 yearsnote 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 about 7 yearsHow do you handle need for elevation?
-
Lalit Bhudiya over 6 yearsIf 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 over 6 yearsOne 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 almost 6 yearsI 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 over 4 yearsI'm getting this error:
[SC] StartService: OpenService FAILED 5: Access is denied.
-
Pramod over 4 yearsMay 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 over 3 yearsWhat do you mean with SERVICE_NAME and APP_SERVICE_NAME ? Could you give an example about that?