Process handling in Java
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   mustafa_adacal
Posted On:   Monday, May 6, 2002 11:52 AM

I want to handle processes in Windows using java. Firstly I create a processes using; Process child = rt.exec("cmd.exe"); Then I want to sequentially send command line commands to the process and get the results. For this purpose I get Input and Output stream of the process by inBuffer = new BufferedReader(new InputStreamReader (child.getInputStream())); outWrite = new PrintWriter(child.getOutputStream(),true); Then I try to invoke a command like "dir" as follows: outWrite.println("dir"); outWrite.flush(); while ( (line = inBuffer.read   More>>


I want to handle processes in Windows using java.
Firstly I create a processes using;


			
Process child = rt.exec("cmd.exe");





Then I want to sequentially send command line commands to the process and get the results. For this purpose I get Input and Output stream of the process by




			

inBuffer = new BufferedReader(new InputStreamReader
(child.getInputStream()));
outWrite = new PrintWriter(child.getOutputStream(),true);





Then I try to invoke a command like "dir" as follows:

			
outWrite.println("dir");
outWrite.flush();
while ( (line = inBuffer.readLine())!= null )
System.out.println(">>>" + line);





But readLine does not return null and the program waits. how can I solve this problem?

   <<Less

Re: Process handling in Java

Posted By:   Dave_Lorde  
Posted On:   Tuesday, May 7, 2002 07:09 AM

The process InputStream will wait for new input, after reading all available input from a command, until the OutputStream is closed, preventing you from sending more commands. So you need to do all the output before reading all the input.


To have a number of commands executed in sequence and get the results of each command, you need to send the series of commands to the OutputStream, then close the OutputStream. Now you can read all the results from all the commands using the InputStream, and it will return null when finished.


You can't break up the sequence of output commands with reads from the InputStream.


Dave

About | Sitemap | Contact