Sign in to follow this  
Belgium

Java. Unable to read more than one line from process

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1305570387' post='4811559']
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.
[/quote]

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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this