0

我的代码如下,我创建了我的 jar 文件。当我从以管理员身份运行的 cmd 执行我的 jar 文件时,jar -jar myfile.jar它可以工作。

public class ServiceStartStop {

private static String SERVICE_NAME = "TestWindowsService";

public static void main(final String[] args) {
    // to start service
    final String[] StartScript = {"cmd.exe", "/c", "sc", "start", SERVICE_NAME};

    try {
        final Runtime runtime = Runtime.getRuntime();
        final Process process = runtime.exec(StartScript);
        final InputStream is = process.getInputStream();
        final InputStreamReader isr = new InputStreamReader(is);
        final BufferedReader br = new BufferedReader(isr);
        String line;

        System.out.printf("Output of running %s is:", Arrays.toString(StartScript));

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

    } catch (final IOException e) {
        e.printStackTrace();
    }
 }
}

我得到这样的输出......

Output of running [cmd.exe, /c, sc, start, TestWindowsService] is:[SC] StartService: OpenService FAILED 5:

Access is denied.

我从这个问题中了解到的是......我cmd.exe正在从不在管理中的java代码打开我的......

我怎么能解决..这个....?

提前致谢....

4

1 回答 1

2
  1. 单击开始并在开始搜索框中键入 CMD
  2. 右键单击列表中的 CMD,然后单击“以管理员身份运行”
于 2013-11-15T10:36:46.057 回答