Java Apache Commons exec输入问题

Java Apache Commons exec输入问题,java,apache-commons-exec,Java,Apache Commons Exec,我试图将一些纯文本键传递给第三方外部程序,然后将输出捕获为字符串。除了第三方程序不接受输入为“正常”外,所有这些似乎都在工作。我得到一个“输入太长”错误。但是,当在bashshell中对相同的二进制文件运行相同的文本集时,它会按预期工作。我似乎找不到任何添加额外字符的内容 我一直遵循这个例子: 这很有效。我可以让stderr打印得很好,甚至可以得到stdout(由于密钥被拒绝,它恰好是空的)任何帮助都是值得感谢的。第三方程序需要在输入流中有一个终止新行(正如echo命令将其输出),因此以下应该可

我试图将一些纯文本键传递给第三方外部程序,然后将输出捕获为字符串。除了第三方程序不接受输入为“正常”外,所有这些似乎都在工作。我得到一个“输入太长”错误。但是,当在bashshell中对相同的二进制文件运行相同的文本集时,它会按预期工作。我似乎找不到任何添加额外字符的内容

我一直遵循这个例子:


这很有效。我可以让stderr打印得很好,甚至可以得到stdout(由于密钥被拒绝,它恰好是空的)任何帮助都是值得感谢的。

第三方程序需要在输入流中有一个终止新行(正如
echo
命令将其输出),因此以下应该可以工作(由@Neurobug确认):


您是否也需要发送换行符?(我想回声是的。)我觉得自己像个白痴。3天来我一直在和这个战斗。谢谢,就这些!
public String run(List<String> keys) throws ExecuteException, IOException{

    //String text = String.join(System.lineSeparator(), keys);  
    String text = "9714917";
    CommandLine cmd = new CommandLine("/third/party/bin/program");

    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    ByteArrayInputStream stdin = new ByteArrayInputStream(text.getBytes("UTF-8"));

    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr, stdin);

    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(new ExecuteWatchdog(10000));
    executor.execute(cmd,environment);


return stdout.toString("UTF-8");
}
echo "9714917" | /third/party/bin/program 
ByteArrayInputStream stdin = new ByteArrayInputStream((text + "\n").getBytes("UTF-8"));