title: How to change stdin when using Runtime class in JAVA
body: <p>I want to use tesseract to recognize some text and I will use <code>Runtime</code> class to exec system command. Here, I want to use stdin to input my img ,rather than reading a file.</p>
  <pre class="lang-java prettyprint-override"><code>    private String preprocessCmdCommand(BufferedImage img) throws IOException, InterruptedException {
          String cmd = &quot;D:\\Program Files\\Tesseract-OCR\\tesseract.exe stdin output -l chi_sm&quot;;
          Runtime run = Runtime.getRuntime();
          try {
              Process p = run.exec(cmd);
              // Write to the standard input stream
              OutputStream stdin = p.getOutputStream();
              stdin.write(HelpFunction.getImageBinary(img, &quot;png&quot;)); //(TesseractOcr.java:40)
              InputStream stdout = p.getInputStream();
              consumeInputStream(stdout);
  
              if (p.waitFor() != 0) {
                  if (p.exitValue() == 1)
                      System.err.println(&quot;fail!&quot;);
              }
          } catch (IOException | InterruptedException e) {
              e.printStackTrace();
          }
          return null;
      }
  </code></pre>
  <p>I tried the method suggested by the first answer , but I got an exception .</p>
  <pre class="lang-java prettyprint-override"><code>java.io.IOException: pipe is closing
      at java.base/java.io.FileOutputStream.writeBytes(Native Method)
      at java.base/java.io.FileOutputStream.write(FileOutputStream.java:347)
      at java.base/java.io.BufferedOutputStream.write(BufferedOutputStream.java:123)
      at java.base/java.io.FilterOutputStream.write(FilterOutputStream.java:108)
      at ocr_processor.TesseractOcr.preprocessCmdCommand(TesseractOcr.java:40)
      at ocr_processor.TesseractOcr.recognizeSingleText(TesseractOcr.java:57)
      at Test.testOrientFunction(Test.java:32)
      at Test.main(Test.java:42)
  [INFO ] 2020-07-16 08:57:42,783 method:Test.testOrientFunction(Test.java:32)
  </code></pre>
  <p>My platform is windows 10 , Java SDK is 14.0.1.</p>