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

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

Introduction

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

Prototype

public void execute(final CommandLine command, final ExecuteResultHandler handler)
        throws ExecuteException, IOException 

Source Link

Usage

From source file:com.tribuneqa.utilities.FrameworkUtilities.java

public static void appiumStop() throws IOException {
    // Add different arguments In command line which requires to stop appium server. 
    CommandLine command = new CommandLine("cmd");
    command.addArgument("/c");
    command.addArgument("taskkill");
    command.addArgument("/F");
    command.addArgument("/IM");
    command.addArgument("node.exe");

    // Execute command line arguments to stop appium server. 
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);/*from   w ww.  j  a  v  a  2s . c  o m*/
    executor.execute(command, resultHandler);
}

From source file:com.kotcrab.vis.editor.util.ApplicationUtils.java

public static void startNewInstance() {
    try {//from   ww w .  j  a v  a 2  s  .co m
        CommandLine cmdLine = CommandLine.parse(getRestartCommand());
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(null, null, null));
        executor.execute(cmdLine, new DefaultExecuteResultHandler());
    } catch (Exception e) {
        Log.exception(e);
    }
}

From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java

public static void exec(final String command, final Map<String, String> environmentVariables,
        final OutputStream outputStream) throws IOException, ExecuteException {
    final Map<?, ?> executionEnvironment = EnvironmentUtils.getProcEnvironment();
    for (final Entry<String, String> environmentVariable : environmentVariables.entrySet()) {
        final String keyAndValue = environmentVariable.getKey() + "=" + environmentVariable.getValue();
        EnvironmentUtils.addVariableToEnvironment(executionEnvironment, keyAndValue);
    }//from   w w  w  . ja  v a  2s . c o  m

    final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);

    final CommandLine commandLine = new CommandLine("/bin/bash");
    commandLine.addArguments(new String[] { "-c", command }, false);

    final DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);
    executor.execute(commandLine, executionEnvironment);
}

From source file:io.selendroid.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PritingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {//from   w  w  w  . jav  a2  s  .c o  m
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        throw new ShellCommandException("An error occured while executing shell command: " + commandline, e);
    }
}

From source file:io.selendroid.standalone.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PrintingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {//  ww  w .  j  a v a2 s .  c o m
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        throw new ShellCommandException("Error executing shell command: " + commandline, e);
    }
}

From source file:com.stratio.ingestion.utils.IngestionUtils.java

public static DefaultExecuteResultHandler executeBash(String command) throws IOException {
    CommandLine cmdLine = CommandLine.parse("bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(command, false);

    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStreamAgentStart = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStreamAgentStart));
    DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
    executor.execute(cmdLine, handler);

    return handler;
}

From source file:com.jaeksoft.searchlib.util.ExecuteUtils.java

final public static int command(File workingDirectory, String cmd, String classpath, boolean setJavaTempDir,
        OutputStream outputStream, OutputStream errorStream, Long timeOut, String... arguments)
        throws ExecuteException, IOException {
    Map<String, String> envMap = null;
    if (classpath != null) {
        envMap = new HashMap<String, String>();
        envMap.put("CLASSPATH", classpath);
    }//from   ww  w.jav a2 s  .  co  m
    CommandLine commandLine = new CommandLine(cmd);
    if (setJavaTempDir)
        if (!StringUtils.isEmpty(SystemUtils.JAVA_IO_TMPDIR))
            commandLine.addArgument(StringUtils.fastConcat("-Djava.io.tmpdir=", SystemUtils.JAVA_IO_TMPDIR),
                    false);
    if (arguments != null)
        for (String argument : arguments)
            commandLine.addArgument(argument);
    DefaultExecutor executor = new DefaultExecutor();
    if (workingDirectory != null)
        executor.setWorkingDirectory(workingDirectory);
    if (outputStream != null) {
        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream);
        executor.setStreamHandler(pumpStreamHandler);
    }
    if (timeOut != null) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeOut);
        executor.setWatchdog(watchdog);
    }
    return envMap != null ? executor.execute(commandLine, envMap) : executor.execute(commandLine);
}

From source file:io.manasobi.utils.CmdUtils.java

/**
 *   (commandLine) ?   ?? ?  ? ?// w  ww  . j a v a2 s .co m
 * 
 * @param commandLine    
 * @param argument     ?
 * @param timeout     
 * 
 * @return int ? 
 */
public static int execute(CommandLine commandLine, String argument, long timeout) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(baos);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);

    // ? 
    if (StringUtils.isNotEmpty(argument)) {
        commandLine.addArguments(argument);
    }

    //  
    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);
    }

    // ??  ? 
    executor.setExitValue(SUCCESS_RETURN_CODE);

    try {

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

        executor.execute(commandLine, resultHandler);
        resultHandler.waitFor();

        return resultHandler.getExitValue();

    } catch (Exception e) {

        throw new CmdUtilsException(e.getMessage());
    }
}

From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java

public static int executeCommand(String command, ExecuteWatchdog watchdog, OutputStream outputStream,
        OutputStream errorStream, InputStream inputStream, Map<String, String> env) {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    if (outputStream == null) {
        outputStream = new LogOutputStream() {
            @Override//  w ww.  ja  va  2s. c om
            protected void processLine(String s, int i) {
                log.error(s);
            }
        };
    }
    if (errorStream == null) {
        errorStream = new LogOutputStream() {
            @Override
            protected void processLine(String s, int i) {
                log.error(s);
            }
        };
    }
    executor.setStreamHandler(new PumpStreamHandler(outputStream, errorStream, inputStream));
    executor.setExitValues(new int[] { 0, 1 });
    if (watchdog != null) {
        executor.setWatchdog(watchdog);
    }
    int exitValue;
    try {
        exitValue = executor.execute(cmdLine, env);
    } catch (IOException e) {
        exitValue = 1;
        LOGGER.error("error executing command", e);
    }
    return exitValue;
}

From source file:com.tribuneqa.utilities.FrameworkUtilities.java

public void appiumStart() throws IOException, InterruptedException {

    // Start command prompt In background.
    CommandLine command = new CommandLine("cmd");

    // Add different arguments In command line which requires to start appium server.
    command.addArgument("/c");

    // Add different arguments In command line which requires to start appium server. 
    command.addArgument("appium");
    command.addArgument("-a");
    command.addArgument("10.20.121.69");
    command.addArgument("-p");
    command.addArgument("8001");
    command.addArgument("-U");
    command.addArgument("4d0081724d5741c7");

    // Execute command line arguments to start appium server. 
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);//www .j a  v a 2 s.  c  o m
    executor.execute(command, resultHandler);

    Thread.sleep(15000);

}