11 Replies Latest reply on Apr 24, 2009 3:23 PM by 807588

    BufferedReader read and readLine methods hanging the application

    807588
      Dear All,
      Please have a look at code.

      import java.io.*;
      public class Snippet {
           public static void transformAction() throws IOException {
                     Process p = null;
                     String batchPath="";
                     batchPath="D:\\ING-TALEND\\JOBS\\FINAL_JOB\\FINAL_JOB_RUN.BAT";
                     p = Runtime.getRuntime().exec("cmd.exe /c " + "\""+batchPath+"\"");
                     InputStream is=p.getInputStream();
                     InputStreamReader isr =      new InputStreamReader(is);
                     String batOutput = "", line="";
                     BufferedReader input = new BufferedReader(isr);
      //read Code
                     while (true) {
                     int nRead = input.read();
                     char c = (char)nRead;
                     System.out.print(c);
                     if (nRead == -1) {
                          System.out.println("break");     
                     break;
                     }
                     }
                     
                     //readLine code
      while ((line = input.readLine()) != null)
                     {
                          batOutput = batOutput + " " + line;
                          System.out.println(batOutput);
                     }
           }
           
           public static void main (String args[]) {
                try {
                     Snippet.transformAction();
                } catch (IOException e) {
                     e.printStackTrace();
                }
           }
      }

      I am trying to run a batch file (a talend job) from java and to display input/output to/from the program.
      When this batch file called from a command prompt, it gives some exceptions but terminates and return to command prompt.
      But when run inside java it got hanged on either readline or read (i tried both).
      It shows all the command to be executed on console, but then got hanged.

      I guess that it is blocking somewhere.
      I searched a lot on web about blocking of readLine and read but haven't got any hint.
      Please suggest any remedies.


      -Sameer


      $@meer
        • 1. Re: BufferedReader read and readLine methods hanging the application
          abillconsl
          I substituted a very simple batch file for yours, one that only executes the DOS 'dir' command. Your code works fine for me, as does a simpler version:
          import java.io.*;
          public class Snippet {
               public static void transformAction() throws IOException {
                    String batchPath="D:\\ING-TALEND\\JOBS\\FINAL_JOB\\FINAL_JOB_RUN.BAT";
                Process p = Runtime.getRuntime().exec("cmd /c start " + batchPath);
            }
          while ((line = input.readLine()) != null) {
          //    You need to comment out this line though if you use the readLine() method:
          //    batOutput = batOutput + " " + line;
               System.out.println(line); //batOutput);
          }
          • 2. Re: BufferedReader read and readLine methods hanging the application
            807588
            Try changing the line
            p = Runtime.getRuntime().exec("cmd.exe /c " + "\""batchPath"\"");
            to
            p = Runtime.getRuntime().exec("cmd.exe /c " + "\"" + batchPath + "\"");
            --janeiros                                                                                                                                                                                                                                                                                                                                                                                                                   
            • 3. Re: BufferedReader read and readLine methods hanging the application
              abillconsl
              janeiros wrote:
              Try changing the line
              p = Runtime.getRuntime().exec("cmd.exe /c " + "\""batchPath"\"");
              to
              p = Runtime.getRuntime().exec("cmd.exe /c " + "\"" + batchPath + "\"");
              --janeiros
              It already is like that - if you look at the code when you 'quote' it you'll see.
              • 4. Re: BufferedReader read and readLine methods hanging the application
                807588
                The following works perfectly:
                package readcommandoutput;
                
                import java.io.BufferedReader;
                import java.io.IOException;
                import java.io.InputStreamReader;
                
                public class Main {
                
                    public static void main(String[] args) throws IOException {
                        
                        String batchPath = "C:\\LS.BAT";
                        Process p = Runtime.getRuntime().exec("cmd.exe /c " + "\"" + batchPath + "\"");
                
                        BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));
                
                        String inputLine = null;
                        while ((inputLine = bf.readLine()) != null) {
                            System.out.println(inputLine);
                        }
                    }
                }
                LS.BAT is:

                @ECHO OFF
                DIR

                --janeiros                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                • 5. Re: BufferedReader read and readLine methods hanging the application
                  abillconsl
                  janeiros wrote:
                  The following works perfectly:
                  package readcommandoutput;
                  
                  import java.io.BufferedReader;
                  import java.io.IOException;
                  import java.io.InputStreamReader;
                  
                  public class Main {
                  
                  public static void main(String[] args) throws IOException {
                  
                  String batchPath = "C:\\LS.BAT";
                  Process p = Runtime.getRuntime().exec("cmd.exe /c " + "\"" + batchPath + "\"");
                  
                  BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));
                  
                  String inputLine = null;
                  while ((inputLine = bf.readLine()) != null) {
                  System.out.println(inputLine);
                  }
                  }
                  }
                  LS.BAT is:

                  @ECHO OFF
                  DIR

                  --janeiros
                  Not to blow my own horn or anything, but this was already mentioned, Jane.
                  • 6. Re: BufferedReader read and readLine methods hanging the application
                    807588
                    There is no issue with path of batch file as i have tested this program with other batch files and got output.
                    For a particular batch file, it is getting hanged on input.read or input.readLine, which batch file (having a java program to run with a lot of parameters ) when executed in command prompt gives some exceptions and exits.
                    What may be the issue? Please respond.

                    -Sameer
                    • 7. Re: BufferedReader read and readLine methods hanging the application
                      793415
                      >
                      ..batch file (having a java program to run with a lot of parameters ) when executed in command prompt gives some exceptions and exits.>
                      The exceptions will normally be dumped to the error stream, not the usual output stream.

                      I suggest using ProcessBuilder, and calling [ProcessBuilder.redirectErrorStream(true)|http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html#redirectErrorStream(boolean)] before creating the process.
                      • 8. Re: BufferedReader read and readLine methods hanging the application
                        807588
                        Thanks Andrew

                        I am getting the error stream later.
                        BufferedReader error = new BufferedReader(
                        new InputStreamReader(p.getErrorStream()));
                        String errorOutput="";
                        while ((line = error.readLine()) != null)
                        {
                        batOutput = batOutput + " " + line;
                        errorOutput = errorOutput + " " + line;
                        }

                        But the case is that it is getting hanged in input.read() or input.readLine() related to getInputStream.
                        Also can you suggest classes prior to Java 5 as i must use jdk1.4.2.

                        -Sameer
                        • 9. Re: BufferedReader read and readLine methods hanging the application
                          807588
                          Dear Andrew,
                          Thanks for the suggestion.
                          I added the following lines
                          ProcessBuilder pb=new ProcessBuilder(batchPath);
                          pb.redirectErrorStream(true);
                          p = pb.start();

                          before InputStream is=p.getInputStream();
                          and it is working fine, but i have to compile the class using JDK5.
                          But I want to do this code on a machine having JDK1.4.2.
                          How can i implement this functionality using JDK1.4.2. Please suggest the relevant classes/snippets if any.

                          Thanks in advance.


                          -Sameer
                          • 10. Re: BufferedReader read and readLine methods hanging the application
                            793415
                            >
                            ...
                            Thanks for the suggestion.
                            I added the following lines
                            ProcessBuilder pb=new ProcessBuilder(batchPath);
                            pb.redirectErrorStream(true);
                            p = pb.start();

                            before InputStream is=p.getInputStream();
                            and it is working fine, but i have to compile the class using JDK5.>
                            I asked you to do that experiment to prove that it was the error stream causing the problem. Check [When Runtime.exec() won't|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html] & particularly for the StreamGobbler that will account for both streams. StreamGobbler should work for pre 1.5 JREs.
                            • 11. Re: BufferedReader read and readLine methods hanging the application
                              807588
                              Dear Andrew,

                              I have added a string field output to StreamGobbler to store output and then used it as follows:

                              p = Runtime.getRuntime().exec("cmd.exe /c " + "\""+batchPath+"\"");

                              StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERR");
                              StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUT");
                              errorGobbler.start();
                              outputGobbler.start();
                              intErrFlg = p.waitFor();     
                                                  
                              batOutput = outputGobbler.output;
                              batOutput = batOutput + " ERROR -- ";
                                                  
                              String errorOutput=errorGobbler.output;
                              if (!errorOutput.equalsIgnoreCase("")){
                              batOutput=batOutput+"<br />"+errorOutput;
                              }

                              This works fine for me.

                              THANKS A LOT.

                              -Sameer