执行我的java代码时拒绝访问cmd

时间:2013-11-15 10:18:43

标签: java windows-services

我的代码如下,我创建了我的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.

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

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

感谢高级......

1 个答案:

答案 0 :(得分:2)

  1. 单击“开始”,然后在“开始搜索框”中键入CMD
  2. 右键单击列表中的CMD,然后单击“以管理员身份运行”