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

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

Introduction

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

Prototype

public DefaultExecutor() 

Source Link

Document

Default constructor creating a default PumpStreamHandler and sets the working directory of the subprocess to the current working directory.

Usage

From source file:client.UglyLaunchTempPatch.java

/**
 * code for lunching external jar file//from w w  w  . j a  va  2s. c  o  m
 * 
 * @param jarFile
 *            the jar file that is being lunched
 * @param Server
 *            is this a server lunch or not
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 * @throws InterruptedException
 */
public static void jar(File jarFile) throws IOException, ClassNotFoundException, NoSuchMethodException,
        InvocationTargetException, IllegalAccessException, InterruptedException {

    main.print("\"" + jarFile.getAbsolutePath() + "\"");
    // sets jar to deleate on exit of program
    jarFile.deleteOnExit();

    //runs the client version of the forge installer.

    Map map = new HashMap();
    map.put("file", jarFile);
    CommandLine cmdLine = new CommandLine("java");
    cmdLine.addArgument("-jar");
    cmdLine.addArgument("${file}");
    cmdLine.setSubstitutionMap(map);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
}

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

public static String exec(CommandLine commandline, long timeoutInMillies) throws ShellCommandException {
    log.info("executing command: " + commandline);
    PritingLogOutputStream outputStream = new PritingLogOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(timeoutInMillies));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    try {//www .j  av a2s  .c  o  m
        exec.execute(commandline);
    } catch (Exception e) {
        throw new ShellCommandException("An error occured while executing shell command: " + commandline,
                new ShellCommandException(outputStream.getOutput()));
    }
    return (outputStream.getOutput());
}

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/* w w w .  ja v  a 2  s . co  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:io.selendroid.standalone.io.ShellCommand.java

public static String exec(CommandLine commandline, long timeoutInMillies) throws ShellCommandException {
    log.info("Executing shell command: " + commandline);
    PrintingLogOutputStream outputStream = new PrintingLogOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(timeoutInMillies));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    try {/*from  ww w  .  ja v  a2 s .com*/
        exec.execute(commandline);
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        if (e.getMessage().contains("device offline")) {
            throw new DeviceOfflineException(e);
        }
        throw new ShellCommandException("Error executing shell command: " + commandline,
                new ShellCommandException(outputStream.getOutput()));
    }
    String result = outputStream.getOutput().trim();
    log.info("Shell command output\n-->\n" + result + "\n<--");
    return result;
}

From source file:net.ishchenko.idea.nginx.platform.NginxCompileParametersExtractor.java

/**
 * Runs file with -V argument and matches the output against OUTPUT_PATTERN.
 * @param from executable to be run with -V command line argument
 * @return Parameters parsed out from process output
 * @throws PlatformDependentTools.ThisIsNotNginxExecutableException if file could not be run, or
 * output would not match against expected pattern
 *//* w w w. j a  v a  2s .  c  om*/
public static NginxCompileParameters extract(VirtualFile from)
        throws PlatformDependentTools.ThisIsNotNginxExecutableException {

    NginxCompileParameters result = new NginxCompileParameters();

    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        executor.setStreamHandler(new PumpStreamHandler(os, os));
        executor.execute(CommandLine.parse(from.getPath() + " -V"));
    } catch (IOException e) {
        throw new PlatformDependentTools.ThisIsNotNginxExecutableException(e);
    }

    String output = os.toString();
    Matcher versionMatcher = Pattern.compile("nginx version: nginx/([\\d\\.]+)").matcher(output);
    Matcher configureArgumentsMatcher = Pattern.compile("configure arguments: (.*)").matcher(output);

    if (versionMatcher.find() && configureArgumentsMatcher.find()) {

        String version = versionMatcher.group(1);
        String params = configureArgumentsMatcher.group(1);

        result.setVersion(version);

        Iterable<String> namevalues = StringUtil.split(params, " ");
        for (String namevalue : namevalues) {
            int eqPosition = namevalue.indexOf('=');
            if (eqPosition == -1) {
                handleFlag(result, namevalue);
            } else {
                handleNameValue(result, namevalue.substring(0, eqPosition),
                        namevalue.substring(eqPosition + 1));
            }
        }

    } else {
        throw new PlatformDependentTools.ThisIsNotNginxExecutableException(
                NginxBundle.message("run.configuration.outputwontmatch"));
    }

    return result;

}

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

/**
 * Exec.//from   www  . j a v  a 2  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:net.gageot.test.utils.Shell.java

/**
 * Execute an external command.// www  .  j av  a2  s  .c o  m
 *
 * @return exitCode or -1 if an exception occured
 */
public int execute(String command, Object... arguments) {
    DefaultExecutor executor = new DefaultExecutor();

    CommandLine commandLine = CommandLine.parse(format(command, arguments));

    try {
        return executor.execute(commandLine);
    } catch (IOException e) {
        return -1;
    }
}

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  ww w .j a va2 s .com*/
    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  w  w .  ja v  a  2 s .  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:com.taobao.ad.es.common.job.executor.ShellHttpJobExecutor.java

@Override
public JobResult execute(JobData jobData) throws IOException {
    JobResult jobResult = JobResult.succcessResult();
    CommandLine cmdLine = CommandLine.parse(jobData.getData().get(JobData.JOBDATA_DATA_JOBCOMMAND));
    Executor executor = new DefaultExecutor();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(1200000);
    executor.setExitValue(0);/*from w w w . j a v a2 s  .  c  om*/
    executor.setWatchdog(watchdog);
    int exitValue = -1;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        exitValue = e.getExitValue();
    }
    if (exitValue != 0) {
        jobResult = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR,
                "Shell?");
        jobResult.setResultCode(exitValue);
        return jobResult;
    }
    jobResult.setResultCode(exitValue);
    return jobResult;
}