• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Program freezes while executing an external command

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Here is my problem:
I am trying to read the output of the php executable. For files that produce small output everything is OK. But when the output gets bigger my java app freezes, waiting for ever. So, I wonder is there a limit to the ammount the stremas can read?
Here is a sample code :

**** test.php ****
[phpcode] echo "Hello World"; [/phpcode]
********************
**** info.php ******
[phpcode]
// Show all information, defaults to INFO_ALL
phpinfo();
[/phpcode]
********************

In the above example, for test.php the program finishes without a problem and I get the correct output. For info.php, the program freezes (obviously it waits for ever at "p.waitFor()". If I press Ctrl+C I see that it has read something like 4000 characters and stopped there.
Any clues?
Tnx,
Tom.
[ November 03, 2003: Message edited by: Tom Diamond ]
[ November 03, 2003: Message edited by: Tom Diamond ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom
I am not sure why it behaves that way but I have one comment to make. That is, I see that you have new String() everytime you read array of byte and add it to the result or error. If you are sure that the data is always String then why not try using StringBuffer? Here, new String() might be just too much load for JVM if there is lot of data you know. You can then use BufferedReader instead of byte stream and then use readLine() method to read data line by line....
Try that and see if you get any further in terms of reading bytes.
Regards
Maulin
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a problem. The available() method is next to useless, as it is unable to distinguish between a stream which has no bytes available yet, and a stream which has no bytes available period because it has reached the end of the stream. There is almost no way to use available() reliably for most applications; just forget you ever saw the method, as it's a very poor API. I'd recommend somehting like:

The reliable way to detect end of file is by checking for a read() return value of -1, as shown. As Maulin suggests, there are also more efficient ways to convert this stuff to a String in the end. Putting a String += into a loop is usually rather inefficient. The above method lets you just create a single String from the complete byte[] array, once it's all been read in. Another method is

[ November 03, 2003: Message edited by: Jim Yingst ]
 
Tom Diamond
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People, thank you!
I tried Jim's solution and it did not work at first. BUT: I put the "p.waitFor()" line just _after_ the loop and everything went fine.
Tnx again,
Tom.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found waitfor pretty useless, too, when I was reading the stdout and errout from the command in other threads. waitfor returned before all the buffered output was read. If you're reading output until -1 you're waiting long enough, I think.
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic