Need a way to check status of Windows service programmatically

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

提交回复
热议问题