当我运行此程序时,它会显示以下文本而不是dir dump为什么?这就是它所显示的内容。
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
它还应该显示dir out put但它现在为什么呢?
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecuteWindowsCommandThread implements Runnable {
public ExecuteWindowsCommandThread(String command) {
}
@Override
public void run() {
try {
final Process p = Runtime.getRuntime().exec("cmd c/ dir");
//p.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
System.out.println("Done2");
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}
答案 0 :(得分:2)
请勿使用c/
而使用/c
,因此,请更改:
final Process p = Runtime.getRuntime().exec("cmd c/ dir");
为:
final Process p = Runtime.getRuntime().exec("cmd /c dir");
它会起作用。