Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2003

Tip: Looking for answers? Try searching our database.

a problem with process.getXXXStream()

Thread view: 
Joana - 14 Sep 2003 16:38 GMT
hello,

I made a small step forward in my problem. But I progress again no
piece: ((( Ask for help...

I have an external process, which makes certain computations for
graph. If the process the 1. Piece of the input in
process.getOutputStream() gets, returns a correct result in
process.getInputStream(). But if the process gets more the next piece
of the input, it does not react at all to it. I.e. I get myself only
the computation, always on the 1. Piece of input refers. No matter,
how many "pieces" the input...

there's the code:

public void dynagraph(){
   Process process = null;
   try{
       process = Runtime.getRuntime().exec("/usr/local/tools/graphviz-1.10/bin/dynagraph
-d -rp");
   }catch (IOException exc){ System.err.println(exc.getMessage());}
       
   PrintWriter pw = new PrintWriter(process.getOutputStream());
   // "open view V" ist das 1. Stueck der Eingabe
   // "end" ist nur die Bezeichnung fuer mich(Es macht in meinem
ganzem
   // Programm)
   String[] lines = new String[]{"open view V", "end"};
   // "open view V", "insert V node a" ist Eingabe aus 2 Stuecken
   // String[] lines = new String[]{"open view V", "insert V node a",
"end"};
   try{
       fw = new FileWriter("/usr/people/bendorai/HDUI/hd/ergebnisse");
       int i = 0;
       while(true){
           String line = lines[i];
           if (line.equals("end"))
               break;
           i++;
           pw.println(line);
           pw.flush();
           readProcessResult(process.getInputStream());
       }
       readProcessResult(process.getInputStream());
       process.destroy();
       pw.close();
       fw.close();
   }catch(IOException e){System.out.println(e.getMessage());}
}

public void readProcessResult(InputStream input) throws IOException {
   int ch = 0;
   // write result to file
   while( (ch=input.read()) != -1){
       fw.write((char)ch);
       fw.flush();
   }
}

thanks,
Joana
Gordon Beaton - 14 Sep 2003 17:07 GMT
> public void readProcessResult(InputStream input) throws IOException {
>     int ch = 0;
[quoted text clipped - 4 lines]
>     }
> }

readProcessResult() will not return until the external program
*closes* his end of the stream, causing read() == -1. In most cases
that will only happen when the process terminates.

If you want a "conversation" with the external process, you have to
use a different condition to know when to return.

A couple of examples:

- does it return one line? Use BufferedReader.readLine() to read only
  a single line.

- does it display a prompt? Recognize the prompt at the start of a
  line.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Gordon Beaton - 14 Sep 2003 17:19 GMT
adding to my own reply...

On 14 Sep 2003 18:07:23 +0200, I wrote:
> If you want a "conversation" with the external process, you have to
> use a different condition to know when to return.

On the other hand, if you don't need to wait for the end of one reply
before sending the next command, use a separate thread to read the
entire reply stream to EOF (read() == -1).

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.