How to check the status of the windows services from a java program?
Asked
Active
Viewed 1.2k times
8
-
1possible duplicate of [Is there any way, in java, to check on the status of a windows service?](http://stackoverflow.com/questions/127299/is-there-any-way-in-java-to-check-on-the-status-of-a-windows-service) – Péter Török Mar 22 '11 at 09:11
-
@Péter Török: This may be a possible dup of the question, but none of the answers to that question touched on the relatively simple ProcessBuilder-based answer I have provided. – Jim Garrison Mar 25 '11 at 01:15
3 Answers
10
on the following example you can find how can you check windws service status and you can parsed to do certain action
import java.util.*;
import java.sql.*;
import java.io.*;
import java.text.*;
public class doscmd
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("sc query browser");
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
if(line.trim().startsWith("STATE"))
{
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("1"))
System.out.println("Stopped");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("2"))
System.out.println("Startting....");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("3"))
System.out.println("Stopping....");
else
if (line.trim().substring(line.trim().indexOf(":")+1,line.trim().indexOf(":")+4).trim().equals("4"))
System.out.println("Running");
}
line=reader.readLine();
}
}
catch(IOException e1) { }
}
}

Waleed
- 101
- 1
- 3
-
The above code returns nothing. When debugged, it says access denied and null. – S K padala Mar 08 '16 at 09:32
4
At the very least you should be able to launch a cmd.exe process with the command sc query service-name
and parse the output to determine the status. Not pretty, but lacking a Java API to the Windows service manager this would be a viable alternative.
EDIT - Read the Javadoc for java.lang.ProcessBuilder, which will allow you to execute an external command. You should probably set the redirectErrorStream property so that you don't have to handle two input streams (stdout and stderr), making for a much simpler design.

Jim Garrison
- 85,615
- 20
- 155
- 190
-
i have googled this sc query,and its works fine.can you please tell me that how can i run this query from java? – Indranil Ghosh Mar 24 '11 at 07:07
-
I edited my answer to include a pointer to the API you'll need to do this. – Jim Garrison Mar 25 '11 at 01:14
2
This method will return true or false depending upon service is running or not.
public boolean checkIfServiceRunning(String serviceName) {
Process process;
try {
process = Runtime.getRuntime().exec("sc query " + serviceName);
Scanner reader = new Scanner(process.getInputStream(), "UTF-8");
while(reader.hasNextLine()) {
if(reader.nextLine().contains("RUNNING")) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}