1

I am attempting to pass in some plain text keys to a third party external program, and then capture the output to a string. All seems to be working, except the third party program is not accepting the input as "normal". I get an "input too long" error. but when running the same set of text to the same binary in a bash shell, it works as intended. I can't seem to find anything that would be appending extra characters.

I have been following this example : How to pipe a string argument to an executable launched with Apache Commons Exec?

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");
}

If I'm correct this should be the same as typing in a shell

echo "9714917" | /third/party/bin/program 

which works. I can get the stderr to print just fine, and even get the stdout ( which just happens to be blank since the key is rejected) Any help is appreciated.

2
  • 1
    Isn't it possible that you need to send a newline character too? (I think echo does.) Commented May 20, 2015 at 17:09
  • 1
    And I feel like an idiot. 3 days I've been fighting with this. Thanks, thats all it was! Commented May 20, 2015 at 17:22

1 Answer 1

1

The 3rd party program needs a terminating new line in the input stream (as the echo command puts to its output), so the following should work (confirmed by @Neurobug):

ByteArrayInputStream stdin = new ByteArrayInputStream((text + "\n").getBytes("UTF-8"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.