我试图在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