title: Failure to start program from java (using ProcessBuilder)
body: <p>I am trying to call cleartool from an java application, but cleartool hangs even for a simple "-version" argument. Running cleardiff instead of cleartool works fine, so apparently there is something specific with the cleartool program (which I assume is related to its interactive capabilities).</p>
  
  <p>The following program</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class ExecTesting extends Thread {
  
      private List&lt;String&gt; command = new ArrayList&lt;String&gt;();
  
      public ExecTesting (List&lt;String&gt; command) {
          super();
          this.command = command;
      }
  
      private void print(String s) {
          System.out.println(s);
      }
  
      @Override
      public void run() {
          Process process;
          OutputStream stdin;
          InputStream stdout;
          InputStream stderr;
          String line;
  
          try {
              String commandString = joinList(command, " ");
              print("Executing: " + commandString);
  
              // runtime.exec has several issues (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1)
              // better to use ProcessBuilder (http://java.sun.com/developer/JDCTechTips/2005/tt0727.html#2)
              //process = Runtime.getRuntime().exec(commandString);
              process = new ProcessBuilder(command).start();
              // it fails in both cases though
  
              stdin  = process.getOutputStream();
              stdout = process.getInputStream();
              stderr = process.getErrorStream();
  
              BufferedReader bufferedStderr = new BufferedReader(new InputStreamReader(stderr));
              while ((line = bufferedStderr.readLine()) != null) {
                  print("stderr: " + line);
              }
              bufferedStderr.close();
  
              BufferedReader bufferedStdout = new BufferedReader(new InputStreamReader(stdout));
              while ((line = bufferedStdout.readLine()) != null) {
                  print("stdout: " + line);
              }
              bufferedStdout.close();
  
              stdin.close();
              stdout.close();
              stderr.close();
  
              process.waitFor();
              print("Execution finished, exit code " + process.exitValue());
              process.destroy();
          } catch (IOException e) {
              print("IOException: " +e.getStackTrace());
          } catch (InterruptedException e) {
              print("InterruptedException: " + e.getStackTrace());
          }
  
      }
  
      /* assumes a list with at least one element */
      private static String joinList(List&lt;String&gt; list, String glue) {
          Iterator&lt;String&gt; i = list.iterator();
          String ret = i.next();
          while (i.hasNext()) {
              ret += glue + i.next();
          }
          return ret;
      }
  
      public static void main(String[] args) {
          ArrayList&lt;String&gt; cmd1 = new ArrayList&lt;String&gt;();
          cmd1.add("c:\\Program Files\\Rational\\ClearCase\\bin\\cleardiff.exe");
          cmd1.add("-version");
          ExecTesting et1 = new ExecTesting(cmd1);
          et1.start();
  
          ArrayList&lt;String&gt; cmd2 = new ArrayList&lt;String&gt;();
          //cmd2.add("c:\\Program Files\\Rational\\ClearCase\\bin\\cleardiff.exe");
          cmd2.add("c:\\Program Files\\Rational\\ClearCase\\bin\\cleartool.exe");
          cmd2.add("-version");
          ExecTesting et2 = new ExecTesting(cmd2);
          et2.start();
  
          et1 = new ExecTesting(cmd1);
          et1.start();
      }
  }
  </code></pre>
  
  <p>gives the following output</p>
  
  <pre><code>Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]
  Executing: [c:\Program Files\Rational\ClearCase\bin\cleartool.exe, -version]
  Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]
  stdout: cleardiff                         2003.06.10+ (Tue Jul 13 14:02:05  2004)
  Execution finished, exit code 0
  </code></pre>
  
  <p>hanging on the execution of the cleartool command. If instead cmd2 is changed to cleardiff the output is as expected</p>
  
  <pre><code>Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]
  Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]
  Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]
  stdout: cleardiff                         2003.06.10+ (Tue Jul 13 14:02:05  2004)
  Execution finished, exit code 0
  stdout: cleardiff                         2003.06.10+ (Tue Jul 13 14:02:05  2004)
  Execution finished, exit code 0
  stdout: cleardiff                         2003.06.10+ (Tue Jul 13 14:02:05  2004)
  Execution finished, exit code 0
  </code></pre>
  
  <p>Question: Does anyone know why cleartool is hanging and how to fix?</p>