Jump to content
  • Advertisement
Sign in to follow this  
Belgium

Java. Unable to read more than one line from process

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!