Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find status of Windows service from Java application?

How to check the status of the windows services from a java program?

like image 997
Indranil Ghosh Avatar asked Mar 22 '11 09:03

Indranil Ghosh


People also ask

How do you check if a process is running in Windows using Java?

If you want to check the work of java application, run 'ps' command with '-ef' options, that will show you not only the command, time and PID of all the running processes, but also the full listing, which contains necessary information about the file that is being executed and program parameters.

Does Java run as a service?

You can install any Java application as a service. Usually, you install applications that run forever like an HTTP server in this case. We use the application as is with just a small change.


1 Answers

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) { } 



   } 
 } 
like image 151
Waleed Avatar answered Sep 22 '22 00:09

Waleed