Need a way to check status of Windows service programmatically

前端 未结 9 884
走了就别回头了
走了就别回头了 2021-01-04 10:11

Here is the situation:

I have been called upon to work with InstallAnywhere 8, a Java-based installer IDE, of sorts, that allows starting and stopping of windows ser

9条回答
  •  有刺的猬
    2021-01-04 10:40

    You can create a small VBS on-th-fly, launch it and capture its return code.

    import java.io.File;
    import java.io.FileWriter;
    
    public class VBSUtils {
      private VBSUtils() {  }
    
      public static boolean isServiceRunning(String serviceName) {
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
    
            String vbs = "Set sh = CreateObject(\"Shell.Application\") \n"
                       + "If sh.IsServiceRunning(\""+ serviceName +"\") Then \n"
                       + "   wscript.Quit(1) \n"
                       + "End If \n"
                       + "wscript.Quit(0) \n";
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("wscript " + file.getPath());
            p.waitFor();
            return (p.exitValue() == 1);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return false;
      }
    
    
      public static void main(String[] args){
        //
        // DEMO
        //
        String result = "";
        msgBox("Check if service 'Themes' is running (should be yes)");
        result = isServiceRunning("Themes") ? "" : " NOT ";
        msgBox("service 'Themes' is " + result + " running ");
    
        msgBox("Check if service 'foo' is running (should be no)");
        result = isServiceRunning("foo") ? "" : " NOT ";
        msgBox("service 'foo' is " + result + " running ");
      }
    
      public static void msgBox(String msg) {
        javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
           null, msg, "VBSUtils", javax.swing.JOptionPane.DEFAULT_OPTION);
      }
    }
    

提交回复
热议问题