title: Run a batch file and then an exe file from Java program
body: <p>I'm having two files that I've to invoke from a java program:</p>
  
  <ol>
  <li>a batch file that sets the environment.</li>
  <li>an exe file </li>
  </ol>
  
  <p>For running the exe, I need to have some environment variables to be set up, which am doing it with the batch file.</p>
  
  <pre><code>set MSDEV_HOME=C:\Program Files\Microsoft Visual Studio 8\VC
  set FOO_ROOT=D:\UGS\Support_Dev\2005SR1
  set FOO_DATA=X:
  call %FOO_DATA%\FOO_profilevars
  set FOO_BIN=B:
  set FOO_LIB=L:
  set FOO_INCLUDE=I:
  </code></pre>
  
  <p><code>FOO_profilevars</code> in the above batch file is another batch file(this also merely sets the environment) that exists on a different hard drive.</p>
  
  <p>Once the environment is set, I'll be calling the exe.</p>
  
  <p>For some reasons, I've to do this separately - i cannot club these two in a another batch file or something and get the things done.</p>
  
  <p>I tried executing:</p>
  
  <pre><code>ProcessBuilder pb = new ProcessBuilder("D:\\newlogin\\setup.bat");
          Process p = pb.start();
          int exitValue = p.waitFor();
          Map&lt;String, String&gt; env = pb.environment();
          System.out.println("exitStatus &gt; " + exitValue);
  
  
  ProcessBuilder pb2 = new ProcessBuilder("d:\\newlogin\\tcelogin.exe",
  "Eid123", "Eid123");        
          Process p2 = pb2.start();
          int exitValue2 = p2.waitFor();
          Map&lt;String, String&gt; env2 = pb2.environment();
          System.out.println("exitStatus &gt; " + exitValue2);
  </code></pre>
  
  <p>This is not working - may because the environment that is set in the first process is not used while executing the second process.
  Is there any way that I execute a file in the environment that is set runtime.</p>
  
  <p><strong>Update:</strong> 
  Why separately means: 
  1. I've some 70env that needs to be set. So, a batch file has been preferred.
  2. The EXE file will return data that needs to be processed.</p>
  
  <pre><code>...
  System.out.println("Process completed");
          BufferedReader reader = null;
          int exitValue = p.exitValue();
          //System.out.println("Exit Value" + exitValue); 
          if(exitValue == 0)
          {
              reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
          }
          else
          {
              reader = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
          }
          StringBuffer sb = new StringBuffer();
          String temp = reader.readLine();
          while(temp != null)
          {
              sb.append(temp);
              temp = reader.readLine();
          }
  
          reader.close();
          System.out.println( sb.toString());
  </code></pre>