Neurobug
Neurobug

Reputation: 11

Apache Commons exec input problems

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.

Upvotes: 1

Views: 1316

Answers (1)

G&#225;bor Bakos
G&#225;bor Bakos

Reputation: 9100

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

Upvotes: 1

Related Questions