title: How can I get error code when I execute an incorrect process?
body: <p>I'm trying to start the powershell script through processbuilder and I'm sure there's an error there, but such commands as exitValue() and waitFor() always return 0 - OK status. Any ideas for catch erros?</p>
  
  <p>P.S. I know that I can parse and view ErrorStream, but..In general, some messages in ErrorStream suit me (they do not affect the script), but there are messages (powershell exeption), like unkonown command or bad command or connections error which should let me know that there was a serious error</p>
  
  <p>P.P.S. my code below:</p>
  
  <pre><code>Process process = null;
  String path = "Path\\To\\PowershellScript.ps1"
      int isErrorProcess = 0;
      int isErrorProcess1 = 0;
      try {
          ProcessBuilder pb = new ProcessBuilder("powershell.exe", path);
          pb.directory(new File(path.substring(0, path.lastIndexOf("\\"))));
          pb.redirectErrorStream(true);
          process = pb.start();            
          ProcessBuilder.Redirect rd = pb.redirectError();
          // Get input streams
          BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(),"866"));
  
          // Read command standard output + error
          String s;
  
          System.out.println("\nOutput:\n");
          while ((s = stdInput.readLine()) != null) {
              System.out.println(s);
          }
  
          isErrorProcess = process.exitValue();
          if(isErrorProcess == 0 &amp;&amp; isErrorProcess1 == 0){ //why always 0 even if powershell script with erros
              isErrorStatus = false;
          }
          process.destroy();
          return isErrorStatus;
  
      } catch (Exception e) {
          process.destroy();
          e.printStackTrace(System.err);
          return true;
      }
  </code></pre>
  
  <p>Many thanks</p>