Need a way to check status of Windows service programmatically

前端 未结 9 885
走了就别回头了
走了就别回头了 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:27

    I have been dealing with installers for years and the trick is to create your own EXE and call it on setup. This offers good flexibility like displaying precise error messages in the event an error occurs, and have success-based return values so your installer knows about what happened.

    Here's how to start, stop and query states for windows services (C++): http://msdn.microsoft.com/en-us/library/ms684941(VS.85).aspx (VB and C# offers similar functions)

    0 讨论(0)
  • 2021-01-04 10:27

    I have had some luck in the past with the Java Service Wrapper. Depending upon your situation you may need to pay in order to use it. But it offers a clean solution that supports Java and could be used in the InstallAnywhere environment with (I think) little trouble. This will also allow you to support services on Unix boxes as well.

    http://wrapper.tanukisoftware.org/doc/english/download.jsp

    0 讨论(0)
  • 2021-01-04 10:35

    A shot in the dark but take a look at your Install Anywhere java documentation.

    Specifically,

    /javadoc/com/installshield/wizard/platform/win32/Win32Service.html

    The class:

    com.installshield.wizard.platform.win32
    Interface Win32Service
    
    All Superinterfaces:
        Service 
    

    The method:

    public NTServiceStatus queryNTServiceStatus(String name)
                                         throws ServiceException
    
        Calls the Win32 QueryServiceStatus to retrieve the status of the specified service. See the Win32 documentation for this API for more information.
    
        Parameters:
            name - The internal name of the service. 
        Throws:
            ServiceException
    
    0 讨论(0)
  • 2021-01-04 10:37

    Simply call this method to check the status of service whether 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;
    }
    
    0 讨论(0)
  • 2021-01-04 10:38

    During startup, create a file with File.deleteOnExit().

    Check for the existence of the file in your scripts.

    0 讨论(0)
  • 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);
      }
    }
    
    0 讨论(0)
提交回复
热议问题