Hello,
I'm trying to read the results of a batch file. Currently, the batch files consists of the following lines;
ECHO Hi
ECHO There
However, my code is only reading the first line (Hi). It never reads "There". Playing around with my while loop it seems to me that readLine() never returns when it is called the second time.
What do I need to do to read more than just the first line of this batch file. Eventually this batch file will call another program which will output stuff other than a greeting.
Thanks.
[source lang="java"]
try
{
String line;
Process p = Runtime.getRuntime().exec(batchFileName);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
input.close();
}
catch (Exception err)
{
err.printStackTrace();
}
[/source]
Java. Unable to read more than one line from process
From playing around with your code, I couldn't reproduce your problem but one thing I found is that you should execute something like "CMD /C <batch file name>". Otherwise the batch file spawns in a long running sub-process that doesn't close.
From playing around with your code, I couldn't reproduce your problem but one thing I found is that you should execute something like "CMD /C <batch file name>". Otherwise the batch file spawns in a long running sub-process that doesn't close.
Crap!
Thanks for running it and letting me know it works. It got me looking in another direction and finally discovered that the .bat file I was editing wasn't actually the batch file I was trying to read with the code. Same name, different folders. I've found that reading the correct file results in the expected behavior.
Anyone know where I can get those four hours back?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement