start windows service from java

后端 未结 5 1369
粉色の甜心
粉色の甜心 2020-12-13 07:05

How can we start/stop a Windows Service from Java? For example, I would like to start and stop the mysql Windows Service from Java.

If start/stop is possible, then i

5条回答
  •  無奈伤痛
    2020-12-13 07:36

    You can formulate a Command Prompt script to start, stop, and check status on a service using a String Array:

    // start service
    String[] script = {"cmd.exe", "/c", "sc", "start", SERVICE_NAME};
    
    // stop service
    String[] script = {"cmd.exe", "/c", "sc", "stop", SERVICE_NAME};
    
    // check whether service is running or not
    String[] script = {"cmd.exe", "/c", "sc", "query", APP_SERVICE_NAME, "|", "find", "/C", "\"RUNNING\""};
    

    Execute scripts using the following:

    Process process = Runtime.getRuntime().exec(script);
    

提交回复
热议问题