简体   繁体   English

从 Java 应用程序查找 Windows 服务的状态?

[英]Find status of Windows service from Java application?

How to check the status of the windows services from a java program?如何从java程序检查windows服务的状态?

on the following example you can find how can you check windws service status and you can parsed to do certain action在下面的示例中,您可以找到如何检查 windws 服务状态以及如何解析以执行某些操作

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



   } 
 } 

At the very least you should be able to launch a cmd.exe process with the command sc query service-name and parse the output to determine the status.至少您应该能够使用命令sc query service-name启动 cmd.exe 进程并解析输出以确定状态。 Not pretty, but lacking a Java API to the Windows service manager this would be a viable alternative.不漂亮,但缺少 Windows 服务管理器的 Java API,这将是一个可行的替代方案。

EDIT - Read the Javadoc for java.lang.ProcessBuilder , which will allow you to execute an external command.编辑 - 阅读java.lang.ProcessBuilder的 Javadoc,这将允许您执行外部命令。 You should probably set the redirectErrorStream property so that you don't have to handle two input streams (stdout and stderr), making for a much simpler design.您可能应该设置redirectErrorStream属性,这样您就不必处理两个输入流(stdout 和 stderr),从而实现更简单的设计。

This method will return true or false depending upon service is running or not.此方法将根据服务是否正在运行返回 true 或 false。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM