请看下面一段代码
Runtime run = Runtime.getRuntime();
Process p = run.exec("C:\WINDOWS\system32\cmd.exe");
BufferedReader br = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String str = null;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
p.getOutputStream()));
Scanner sc = new Scanner(System.in);
while (true) {
while ((str = br.readLine()) != null) {
System.out.println("--" + str);
}
String cmd = sc.nextLine();
System.out.println(cmd);
if (cmd.equalsIgnoreCase("q") || cmd.equalsIgnoreCase("exit")) {
break;
}
bw.write(cmd);
bw.flush();
}
p.destroy();
bw.close();
br.close();
把 你代码改了下, 解决了你的问题
你手动打开 cmd 试试.进去就是有消息要输出的 所以你第一次不需要去
String cmd = sc.nextLine();
我的方法是做了一次判断, 吧String cmd 放在了while 外面定义
String cmd = null;
while (true) {
if (cmd != null){
cmd = sc.nextLine();
System.out.println(cmd);
} else {
cmd = "";
}
.
.
.
}
这样就不会有那个问题了。 我刚测试了下.
:( 控制台不能这么模仿cmd 吧.. java只能启用.exe
就是你顶多能够弹出 dos窗口 怎么可能在控制台给你输出呢!
控制台输出 都是能 .tostring()的东东。