Example usage for org.apache.commons.exec CommandLine parse

List of usage examples for org.apache.commons.exec CommandLine parse

Introduction

In this page you can find the example usage for org.apache.commons.exec CommandLine parse.

Prototype

public static CommandLine parse(final String line) 

Source Link

Document

Create a command line from a string.

Usage

From source file:com.cprassoc.solr.auth.util.CommandLineExecutor.java

public static int exec(String cmd) {
    int exitValue = -1;
    try {/*  w w  w .ja  va  2 s .c o m*/
        CommandLine cmdLine = CommandLine.parse(cmd);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
        executor.setWatchdog(watchdog);
        exitValue = executor.execute(cmdLine);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return exitValue;
}

From source file:eu.forgestore.ws.util.Utils.java

public static int executeSystemCommand(String cmdStr) {

    CommandLine cmdLine = CommandLine.parse(cmdStr);
    final Executor executor = new DefaultExecutor();
    // create the executor and consider the exitValue '0' as success
    executor.setExitValue(0);//from w w w . ja v  a  2s . co  m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(out);
    executor.setStreamHandler(streamHandler);

    int exitValue = -1;
    try {
        exitValue = executor.execute(cmdLine);

    } catch (ExecuteException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    return exitValue;

}

From source file:au.com.jwatmuff.genericp2p.windows.ExecUtil.java

public static String getStdoutForCommand(String cmd) {
    CommandLine cmdLine = CommandLine.parse(cmd);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWatchdog(new ExecuteWatchdog(60000));
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(stdout, null));
    try {/*from   w w  w.  j  a va  2s  . c  o m*/
        executor.execute(cmdLine);
    } catch (ExecuteException e) {
        log.error("Exception executing '" + cmd + "'", e);
    } catch (IOException e) {
        log.error("IOException executing '" + cmd + "'", e);
    }

    return stdout.toString();
}

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

/**
 * ? ?  ? ?//from  w  w  w . ja va 2s.  co m
 * 
 * @param line    
 * @return int ? 
 */
public static int execute(String line) {

    CommandLine commandLine = CommandLine.parse(line);

    return execute(commandLine, null);
}

From source file:com.tibco.tgdb.test.utils.ProcessCheck.java

/**
 * Check whether a process is running or not
 * @param pid pid of the process to monitor
 * @return true if process is running/*from   www  . j  av a 2 s.  c  o  m*/
 * @throws IOException IO exception
 */
public static boolean isProcessRunning(int pid) throws IOException {
    String line;
    if (OS.isFamilyWindows()) {
        //tasklist exit code is always 0. Parse output
        //findstr exit code 0 if found pid, 1 if it doesn't
        line = "cmd /c \"tasklist /FI \"PID eq " + pid + "\" | findstr " + pid + "\"";
    } else {
        //ps exit code 0 if process exists, 1 if it doesn't
        line = "ps -p " + pid;
    }
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(null, null, null));
    executor.setExitValues(new int[] { 0, 1 });
    int exitValue = executor.execute(cmdLine);
    if (exitValue == 0)
        return true;
    else if (exitValue == 1)
        return false;
    else // should never get to here in theory since execute would throw exception
        return true;
}

From source file:com.impetus.ankush.common.utils.CommandExecutor.java

/**
 * Exec./*from  ww  w.j  av a2  s  . c  o m*/
 *
 * @param command the command
 * @param out the out
 * @param err the err
 * @return the int
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws InterruptedException the interrupted exception
 */
public static int exec(String command, OutputStream out, OutputStream err)
        throws IOException, InterruptedException {
    CommandLine cmdLine = CommandLine.parse(command);
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(out, err, null));
    return executor.execute(cmdLine);
}

From source file:io.github.binout.wordpress2html.writer.Html2AsciidocConverter.java

public Html2AsciidocConverter() throws IOException {
    execute(CommandLine.parse("pandoc --version"));
}

From source file:fitnesse.maven.io.CommandShell.java

public String execute(File workingDir, String... commands) {
    CommandLine commandLine = CommandLine.parse(commands[0]);
    for (int i = 1; i < commands.length; i++) {
        commandLine.addArgument(commands[i]);
    }//from w w w.  ja va  2s  .  c o  m
    DefaultExecutor executor = new DefaultExecutor();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(baos));
        executor.setWorkingDirectory(workingDir);
        executor.execute(commandLine);
        return new String(baos.toByteArray());
    } catch (ExecuteException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.bptselenium.jenkins.BPTSeleniumJenkins.RunCommand.java

public static void runCommand(String command, TaskListener listener, Run<?, ?> build) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(command);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    Executor executor = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.setExitValue(10);//from w ww.j  ava  2s  . c o  m
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine);
    } catch (ExecuteException ee) {
        //getting a non-standard execution value, set build result to unstable
        Result result = Result.UNSTABLE;
        if (build.getResult() == null) {
            build.setResult(result);
        } else if (build.getResult().isBetterThan(result)) {
            build.setResult(result.combine(build.getResult()));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        listener.getLogger().println(outputStream.toString());
    }
}

From source file:de.akquinet.innovation.play.maven.Play2RunMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("run");
    DefaultExecutor executor = new DefaultExecutor();

    // As where not linked to a project, we can't set the working directory.
    // So it will use the directory where mvn was launched.

    executor.setExitValue(0);//w w  w. ja  v  a2  s. c  om
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}