2 Replies Latest reply: Dec 11, 2013 10:08 AM by rukbat RSS

    Last line is still not read

    meandmycode

      Hi,

       

      I fixed error in last post and rewrote the code using ProcessBuilder instead.

      Output from my unix tool is:

       

      Command is a shell application' /home/myid/test/myshell/bin/run' that starts a ascii gui that looks like ( see below). It waits for input from user.

       

      *******************************************
      *   My shell Application                   *

      *******************************************

       

      HELP: h

      COMMAND: c

      QUIT:q

      135.19.45.18>

       

      And still the last line is not showing when I read the text.

      I have tried with readLine() and I get the same error.

      Note: When I debug, after characters in line:

      OUIT:q

      have been read I can see that debugger hangs on

      br.read()

       

      And now the code looks like:

       

      
      
      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.util.ArrayList;
      import java.util.List;
      
      
      public class RunShellCmd {
      
          private BufferedReader processInputStream;
          private BufferedWriter processWriter;
      
          public static void main(String args[]) throws Exception {
              RunShellCmd cmd = new RunShellCmd();
              cmd.runCmd();
          }
      
          public void runCmd() throws Exception {
      
              Process process = null;
      
              List<String> cmd = new ArrayList<String>();
              cmd.add("/bin/bash");
              cmd.add("/home/myid/test/myshell/bin/run");
            
      
              ProcessBuilder processBuilder = new ProcessBuilder(cmd);
              processBuilder.redirectErrorStream(true);
      
              try {
                  System.out.println("start process ....");
                  process = processBuilder.start();
                  InputStream is = process.getInputStream();
                  InputStreamReader isr = new InputStreamReader(is);
                  BufferedReader br = new BufferedReader(isr);
                  StringBuilder sb = new StringBuilder();
      
      
                  int intch;
                  while ((intch = br.read()) != -1) {
                    char ch = (char) intch;
                    sb.append(ch);
                    System.out.println(sb.toString());
                  }
      
              
      
              } catch (IOException e) {
                  System.out
                          .println("An error occourd: "
                                  + e.toString());
              } finally {
      
                  processInputStream.close();
                  process.destroy();
                  System.out.println("exit value: " + process.exitValue());
                  processWriter.close();
              }
          }
      
      }