2

我现在有 Windows 服务我想使用 jsp 页面停止和启动窗口服务意味着,在我的 jsp 页面中有一个按钮启动和停止,当我们单击这些按钮时它应该是停止和启动服务,我使用这样的一些命令

public static int stopService(String serviceName) throws Exception {

return execCmd("cmd /c net stop \" + serviceName + "\"");

}

请任何人对此提供一些指导,谢谢你提前

4

1 回答 1

2

使用过程。

Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();

详细的编码工作

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

public class program7 {

    public static void main(String args[]) {
        String serviceName = ""; // Insert your service name
        try {
            stopService(serviceName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void stopService(String serviceName) throws IOException,
            InterruptedException {

        String executeCmd = "cmd /c net stop \"" + serviceName + "\"";
        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();

        System.out.println("processComplete: " + processComplete);

        if (processComplete == 1) {// if values equal 1 process failed
            System.out.println("Service failed");
        }

        else if (processComplete == 0) {
            System.out.println("Service Success");
        }

    }
}
于 2012-10-03T04:44:17.167 回答