Example usage for org.apache.commons.exec DefaultExecutor getStreamHandler

List of usage examples for org.apache.commons.exec DefaultExecutor getStreamHandler

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecutor getStreamHandler.

Prototype

public ExecuteStreamHandler getStreamHandler() 

Source Link

Usage

From source file:org.estatio.webapp.services.other.EstatioOtherServices.java

@Programmatic
String execute(String command) {

    int exitValue = 1;
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);//  w w  w .j  a  va2s  . c  o  m
    ExecuteStreamHandler handler = executor.getStreamHandler();

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    executor.setStreamHandler(psh);

    try {
        handler.setProcessOutputStream(System.in);
    } catch (IOException e) {

    }

    try {
        exitValue = executor.execute(commandLine);
    } catch (ExecuteException e) {
        return e.getMessage();
    } catch (IOException e) {
        return e.getMessage();
    }
    return stdout.toString();
}