使用jsp页面启动和停止Windows服务

时间:2012-10-03 04:29:46

标签: java jsp windows-services

我有Windows服务现在我想停止并使用jsp页面启动窗口服务,在我的jsp页面中有一个按钮启动和停止当我们点击那些按钮然后它应该停止并启动服务,我使用一些像这样的命令

public static int stopService(String serviceName) throws Exception {

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

}

请任何人提供一些指导,谢谢你inadvance

1 个答案:

答案 0 :(得分: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");
        }

    }
}