无法运行带有参数'cmd'命令'sc sdshow w32time'



我试图在Java中运行外部进程,我不知道为什么我的代码不工作。它适用于任何其他'cmd'命令(例如/c dir)。如果我用cmd /c dir代替cmd sc sdshow w32time,它可以工作。

下面是我的代码:
public class services2 {
    public static void main(String args[]) {
        try {
            Process p = Runtime.getRuntime().exec("cmd sc sdshow w32time");
            p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
        } catch (IOException e1) {
        } catch (InterruptedException e2) {
        }
        System.out.println("Done");
    }
}

任何想法?

你的问题似乎是sc不是cmd的论据。
你需要的是:

sc sdshow w32time

最新更新