• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Reading output from a System.exec Process

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm using a util called SX to convert SGML content into XML content, which is a binary file run from a command prompt. I'd like to automate the process so that my application can take an SGML file and internally do the conversion without having to create a temp file to copy the XMl content to.
The SX command returns the XML content to the command prompt, but that can be redirected to a file. What I'd like to do is use System.exec to start SX, then use the InputStream from the Process to get the XML content of the file. However, whenever I do this the Process simply hangs and I get nothing. There's no error, it simply stops executing.
Has anyone got any ideas, or sample code they could show me on a proper way to use a Process?
Thanks,
-tim
 
Bob Dobalina
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Any ideas, or is everybody on vacation?
-tim
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the sample code
<pre>
import java.io.*;
public class ls
{
public static void main(String a[])
{
try{
Process p = Runtime.getRuntime().exec(ls);
DataInputStream dis = new DataInputStream(p.getInputStream());
String line = "";
while ( (line = dis.readLine()) != null)
{
System.out.println(line);
}
dis.close();

}catch(Exception e)
{
e.printStackTrace();
}
}

}
</pre>



[This message has been edited by Aleksey Matiychenko (edited July 06, 2001).]
 
Bob Dobalina
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks, that's pretty close to what I was doing, except I wasn't using a DataInputStream. I thought perhaps that was my problem, but I modified your app to call the "sx" executable, and it still just sits there... But, it does work for "ls"
Q, did you intend to have
Process p = Runtime.getRuntime().exec(ls);
Or did you mean to have:
Process p = Runtime.getRuntime().exec("ls");
The former option doesn't work for me.
Anyway, I'll keep playing... thanks.
-tim
 
Bob Dobalina
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It was suggested to me that the input is simply blocking because it's receiving too much text too fast... and that's hanging things up. Anybody have any reliable ways around this? By using a reader thread and adding the text to a StringBuffer I can get all the output from the sx app, but if I make a small change like adding the text to a String rather than a StringBuffer, the app hangs...
-tim
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm - you might try wrapping the InputStream in a BufferedInputStream. That should allow you to read at a more leisurely pace, if the buffer is large enough. It might also be a good idea to check the error stream on the process - there may be important info there which you're missing. The problem is, while you're reading one stream, the application may hang if there's a backlog on the other stream. Very annoying. I suggest creating a separate thread to read the error stream, and print any info there to either the screen, or a log file somewhere. (Or both.)
Incidentally, note that DataInputStream's readLine() method has been deprecated, and BufferedInputStream is the recommended replacement.
 
Bob Dobalina
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only way I can get it to work, it seems, is by starting threads to read the output and the error streams separately. Even buffering the output with a buffer larger than the data to be outputted didn't help matters, disappointingly.
So, threads it is. Thanks all,
-tim
p.s. I should clarify that by stating that there needs to be separate threads, one to read the output of the process and a second to read the error output. I tried doing both in a single thread and that locked up tight too...
[This message has been edited by Tim Stevens (edited July 09, 2001).]
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this work for you:
 
permaculture is giving a gift to your future self. After reading this 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