Find status of Windows service from Java application?

前端 未结 3 660
忘了有多久
忘了有多久 2021-01-13 05:59

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

3条回答
  •  忘掉有多难
    2021-01-13 06:24

    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;
    }
    

提交回复
热议问题