title: Java Powershell CreateProcess error=2, The system cannot find the file specified
body: <p>I am executing powershell commands in java and I have written two programs, however the strange part is one works fine and the other throws the error. The code that throws the error is as shown </p>
  
  <p>I have tried the following
  1) Spcifying the fully specified path of powershell
  2) My path variable has the following - "C:\WINDOWS\system32\WindowsPowerShell\v1.0"</p>
  
  <p>I know I might be doing something trivial but its been a day and I am unable to figure out what the issue might be</p>
  
  <pre><code>import java.io.IOException;
  
  public class FileCount {
  
  public static void main(String[] args) {
      Process flCntProcess = null;
      try {
  
          String test  = "C:\\WINDOWS\\system32\\windowspowershell\\v1.0\\powershell.exe  -Command \"&amp; { Get-ChildItem C:\\test -Recurse -force | Measure-Object }\"";
          System.out.println("Powershell command : " + test);
          ProcessBuilder builder = new ProcessBuilder(test);
          builder.redirectErrorStream(true);
          flCntProcess = builder.start();
  
          //  FILE COUNT OUTPUT STREAM PROCESSING
          NotifyThreadComplete outputThread = new ProcessHandler(flCntProcess.getInputStream(),"OUTPUT");
          outputThread.addListener(new ThreadCompleteListener() {
  
              @Override
              public void notifyCompletion(Thread t, long startTm, boolean didErrorOut, String noOfLines) {
                  System.out.println("Completed Output Stream Processing");
                  System.out.println("Printing values");
                  System.out.println("No of Lines : " + noOfLines);
                  System.out.println("Did Error out : " + didErrorOut);
  
                  if(didErrorOut) {
                      System.out.println("Do not continue with processing");
                  } else {
                      System.out.println("Continue with processing");
                  }
              }
          });
          System.out.println("Starting output thread ");
          outputThread.start();
  
      } catch (Exception e) {
          System.err.println("Exception while counting files using Powershell Command" + e.getMessage());
      } finally {
          if(flCntProcess != null &amp;&amp; flCntProcess.getOutputStream() != null) {
              try {
                  flCntProcess.getOutputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  }
  </code></pre>