【发布时间】:2013-11-14 03:06:25
【问题描述】:
我编写了一个简单的 Java 程序,它每 5 秒输出一些“hello”到 std。
public class DDD {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; ; i++) {
System.out.println("hello " + i);
Thread.sleep(5000);
}
}
}
然后我编译它并得到一个 .class。
我写了另一个java程序来运行它并得到输出:
public static void main(String[] args) throws Exception {
String command = "c:\\java\\jdk1.7.0_07\\bin\\java mytest.DDD";
Process exec = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
while (true) {
String line = reader.readLine();
System.out.println(line);
if (line == null) {
Thread.sleep(1000);
}
}
}
但它总是打印:
null
null
null
null
null
null
null
哪里错了?我的操作系统是“windows XP”。
【问题讨论】:
-
主要是因为到了输入流的末尾。这表明进程失败并已将原因写入错误流,但您没有阅读它。
标签: java command-line std