title: How to pipe a string argument to an executable launched with Apache Commons Exec?
body: <p>I need to pipe a text argument to the stdin of a command launched with Apache Commons Exec (for the curious, the command is gpg and the argument is the passphrase to the keystore; gpg does not have an argument to provide the passphrase explicitly, only to accept it from stdin).</p>
  
  <p>In addition, I need this to support both Linux and Windows.</p>
  
  <p>In a shell script I'd do </p>
  
  <pre><code>cat mypassphrase|gpg --passphrase-fd
  </code></pre>
  
  <p>or </p>
  
  <pre><code>type mypassphrase|gpg --passphrase-fd
  </code></pre>
  
  <p>but type doesn't work on Windows as it's not an executable but a command built into the command interpreted (cmd.exe).</p>
  
  <p>The <em>code</em> not working (for the above reason) is below. To spawn an entire shell for this is too ugly, I was looking for a more elegant solution. Unfortunately, there are some incompatibility problems between the BouncyCastle library and PGP so I cannot use a fully programmatic solution in the (very short) time I have. </p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>CommandLine cmdLine = new CommandLine("type");
  cmdLine.addArgument(passphrase);
  cmdLine.addArgument("|");
  cmdLine.addArgument("gpg");
  cmdLine.addArgument("--passphrase-fd");
  cmdLine.addArgument("0");
  cmdLine.addArgument("--no-default-keyring");
  cmdLine.addArgument("--keyring");
  cmdLine.addArgument("${publicRingPath}");
  cmdLine.addArgument("--secret-keyring");
  cmdLine.addArgument("${secretRingPath}");
  cmdLine.addArgument("--sign");
  cmdLine.addArgument("--encrypt");
  cmdLine.addArgument("-r");
  cmdLine.addArgument("recipientName");
  cmdLine.setSubstitutionMap(map);
  DefaultExecutor executor = new DefaultExecutor();
  int exitValue = executor.execute(cmdLine);
  </code></pre>