我试图使用java程序运行命令,但p.waitfor()函数永远等待.代码有什么问题?
import java.io.*;
public class doscmd
{
public static void main(String args[]) throws InterruptedException
{
try
{
Process p=Runtime.getRuntime().exec("cmd /c dir");
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) {}
System.out.println("Done");
}
}
Run Code Online (Sandbox Code Playgroud)
目录大吗?也许p填满了它的输出缓冲区并停止等待读者消费一些东西,所以它可以完成写出目录列表.
你可能应该搬家
p.waitFor();
Run Code Online (Sandbox Code Playgroud)
到方法的最后.