Java – p.waitfor() wait forever
•
Java
I tried to run the command using a java program, but the p.waitfor() function never waited What's wrong with the code?
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"); } }
Solution
Is the catalog big? Perhaps P fills its output buffer and stops waiting for readers to consume something, so it can finish writing out the directory list
Maybe you should move
p.waitFor();
To the end of the method
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码