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

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

Introduction

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

Prototype

public void setWorkingDirectory(final File dir) 

Source Link

Usage

From source file:uk.org.sappho.applications.transcript.service.registry.vcs.CommandExecuter.java

public String execute(Command command, File directory) throws TranscriptException {

    try {/*w  w w  .  j  a  va2  s. co  m*/
        if (logger.isLoggable(Level.INFO)) {
            logger.info(command.getSafeCommand());
        }
        DefaultExecutor executor = new DefaultExecutor();
        DefaultExecuteResultHandler commandResultsHandler = new DefaultExecuteResultHandler();
        ByteArrayOutputStream commandOutputStream = new ByteArrayOutputStream();
        ByteArrayOutputStream commandErrorStream = new ByteArrayOutputStream();
        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(commandOutputStream, commandErrorStream);
        executor.setWatchdog(new ExecuteWatchdog(60000));
        executor.setStreamHandler(pumpStreamHandler);
        executor.setWorkingDirectory(directory);
        executor.execute(command.getCommandLine(), commandResultsHandler);
        commandResultsHandler.waitFor();
        if (commandResultsHandler.getExitValue() != 0) {
            throw new TranscriptException(commandErrorStream.toString());
        }
        return commandOutputStream.toString();
    } catch (Throwable throwable) {
        if (logger.isLoggable(Level.WARNING)) {
            logger.warning(throwable.getMessage());
        }
        throw new TranscriptException("Unable to execute system command: " + command.getSafeCommand(),
                throwable);
    }
}

From source file:wf.frk.tilde.sblauncher.SauerbratenExecutor.java

public void startThread(String executable, String[] params, String working_dir)
        throws ExecuteException, IOException {

    setDaemon(true);//  www . j av a2s.  c  o m
    startThread();

    CommandLine cmdLine = CommandLine.parse(executable);
    for (String p : params) {
        System.out.println(p);

        cmdLine.addArgument(p);
    }

    DefaultExecutor executor = new DefaultExecutor();

    PipedOutputStream output = new PipedOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(output, System.err);
    SAUER_OUTPUT = new Scanner(new PipedInputStream(output));

    executor.setStreamHandler(streamHandler);
    executor.setWorkingDirectory(new File(working_dir));
    executor.execute(cmdLine);

}