一切在命令行上都可以正常运行,但是当我将所需的内容转换为Java时,接收过程在stdin上什么都收不到。
这是我所拥有的:
private void deployWarFile(File warFile, String instanceId) throws IOException, InterruptedException { Runtime runtime = Runtime.getRuntime(); // FIXME(nyap): Use Jsch. Process deployWarFile = runtime.exec(new String[]{ "ssh", "gateway", "/path/to/count-the-bytes"}); OutputStream deployWarFileStdin = deployWarFile.getOutputStream(); InputStream deployWarFileStdout = new BufferedInputStream(deployWarFile.getInputStream()); InputStream warFileInputStream = new FileInputStream(warFile); IOUtils.copy(warFileInputStream, deployWarFileStdin); IOUtils.copy(deployWarFileStdout, System.out); warFileInputStream.close(); deployWarFileStdout.close(); deployWarFileStdin.close(); int status = deployWarFile.waitFor(); System.out.println("************ Deployed with status " + status + " file handles. ************"); }
脚本“ count-the-bytes”很简单:
#!/bin/bash echo "************ counting stdin bytes ************" wc -c echo "************ counted stdin bytes ************"
输出表明该函数挂在’wc -c’行-永远不会到达’counted stdin bytes’行。
这是怎么回事?使用Jsch会有所帮助吗?
您可能希望在wc -c返回之前尝试关闭输出流。
IOUtils.copy(warFileInputStream, deployWarFileStdin); deployWarFileStdin.close(); IOUtils.copy(deployWarFileStdout, System.out); warFileInputStream.close(); deployWarFileStdout.close();