关于Runtime.exec()的OutputStream问题
我的程序
public void run() {
try {
process = Runtime.getRuntime().exec( "cmd /c dir ");
// Process process = Runtime.getRuntime().exec( "java WEB-INF.classes.chan.Hello.Test2 ");
// Process process = Runtime.getRuntime().exec( "cmd /c dir ");
print();
Thread.sleep(1000);
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.println( "dir ");
print();
Thread.sleep(1000);
pw.println( "cd / ");
print();
Thread.sleep(1000);
pw.println( "dir ");
print();
Thread.sleep(1000);
// process.wait();
} catch (Exception e) {
e.printStackTrace();
}
// while(isStop)
// ;
// destroy();
// System.out.println( "been destroyed! ");
}//run
其中print()方法是:
public void print() {
String line = " ";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
while ((line = reader.readLine()) != null)
System.out.println(line);
System.out.println( "============= ");
BufferedReader error = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
while ((line = error.readLine()) != null)
System.out.println(line);
} catch (Exception e) {
e.printStackTrace();
}
}//print
=====================
这只是一个小测试,目的是想用PrintWriter向进程中写入字串。
但是最终输出的结果是第一个【dir】有结果,然后后面【dir】、【cd /】、【dir】都之是打印出了“=====”!
不解!
------解决方案--------------------pw.println( "dir ");这句你想把DIR命令传到process中,但这没有用。
------解决方案--------------------我以前也试过这样写 不过好像不行。。 学习一下
------解决方案--------------------IO流中断,当然要停在while ((line = reader.readLine()) != null)这一行,