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

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

Introduction

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

Prototype

public CommandLine(final CommandLine other) 

Source Link

Document

Copy constructor.

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);//w  w w  . j av a2  s  .  c  o m
    executor.execute(command, resultHandler);
}

From source file:eu.creatingfuture.propeller.webLoader.propeller.OpenSpin.java

/**
 * https://code.google.com/p/open-source-spin-compiler/wiki/CommandLine
 *
 * @param executable/*from  w  ww .  j  a  v a 2  s .c  o  m*/
 * @param sourceFile
 * @return
 */
protected boolean compile(String executable, File sourceFile) {
    try {
        File temporaryDestinationFile = File.createTempFile("blocklyapp", ".binary");
        File libDirectory = new File(new File(System.getProperty("user.dir")), "/propeller-lib");
        Map map = new HashMap();
        map.put("sourceFile", sourceFile);
        map.put("destinationFile", temporaryDestinationFile);
        map.put("libDirectory", libDirectory);

        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-o").addArgument("${destinationFile}");
        cmdLine.addArgument("-L").addArgument("${libDirectory}");
        cmdLine.addArgument("${sourceFile}");
        cmdLine.setSubstitutionMap(map);
        DefaultExecutor executor = new DefaultExecutor();
        //  executor.setExitValues(new int[]{402, 101});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            success = false;
            return false;
        } finally {
            temporaryDestinationFile.delete();
            output = outputStream.toString();
        }

        //            System.out.println("output: " + output);
        /*
         Scanner scanner = new Scanner(output);
                
                
         Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?");
         Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$");
         while (scanner.hasNextLine()) {
         String portLine = scanner.nextLine();
         if (chipFoundPattern.matcher(portLine).matches()) {
         Matcher portMatch = pattern.matcher(portLine);
         if (portMatch.find()) {
         //   String port = portMatch.group("comport");
                
         }
         }
         }
         */
        //            System.out.println("output: " + output);
        //            System.out.println("exitValue: " + exitValue);
        success = true;
        return true;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        success = false;
        return false;
    }
}

From source file:eu.creatingfuture.propeller.blocklyprop.propeller.OpenSpin.java

/**
 * https://code.google.com/p/open-source-spin-compiler/wiki/CommandLine
 *
 * @param executable/* w w w  .  jav  a 2  s  .  co m*/
 * @param sourceFile
 * @return
 */
protected boolean compile(String executable, File sourceFile) {
    try {
        File temporaryDestinationFile = File.createTempFile("blocklyapp", ".binary");
        File libDirectory = new File(new File(System.getProperty("user.dir")), "/propeller-lib");
        Map map = new HashMap();
        map.put("sourceFile", sourceFile);
        map.put("destinationFile", temporaryDestinationFile);
        map.put("libDirectory", libDirectory);

        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-o").addArgument("${destinationFile}");
        cmdLine.addArgument("-L").addArgument("${libDirectory}");
        cmdLine.addArgument("${sourceFile}");
        cmdLine.setSubstitutionMap(map);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(new int[] { 0, 1 });

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            success = false;
            return false;
        } finally {
            temporaryDestinationFile.delete();
            output = outputStream.toString();
        }

        //            System.out.println("output: " + output);
        /*
         Scanner scanner = new Scanner(output);
                
                
         Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?");
         Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$");
         while (scanner.hasNextLine()) {
         String portLine = scanner.nextLine();
         if (chipFoundPattern.matcher(portLine).matches()) {
         Matcher portMatch = pattern.matcher(portLine);
         if (portMatch.find()) {
         //   String port = portMatch.group("comport");
                
         }
         }
         }
         */
        //            System.out.println("output: " + output);
        //            System.out.println("exitValue: " + exitValue);
        success = true;
        return true;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        success = false;
        return false;
    }
}

From source file:io.selendroid.builder.SelendroidServerBuilderTest.java

@Test
public void testShouldBeAbleToCreateCustomizedSelendroidServerAndCleantTUp() throws Exception {
    SelendroidServerBuilder builder = getDefaultBuilder();
    builder.init(new DefaultAndroidApp(new File(APK_FILE)));
    builder.cleanUpPrebuildServer();//  w ww  . j  av  a 2 s. c  om

    // Verify apk, if the files have been removed
    CommandLine cmd = new CommandLine(AndroidSdk.aapt());
    cmd.addArgument("list", false);
    cmd.addArgument(builder.getSelendroidServer().getAbsolutePath(), false);

    String output = ShellCommand.exec(cmd);

    assertResultDoesNotContainFile(output, "META-INF/CERT.RSA");
    assertResultDoesNotContainFile(output, "META-INF/CERT.SF");
    assertResultDoesNotContainFile(output, "AndroidManifest.xml");
    // just double check that dexed classes are there

    assertResultDoesContainFile(output, "classes.dex");
}

From source file:edu.buffalo.fusim.BowtieAlignment.java

public void run(String bowtiePath, String index, File read1, File read2, File outFile, int threads) {
    // Example bowtie call:
    //   bowtie -t -v 2 -p 12 -m 10 -S
    CommandLine cmdLine = new CommandLine(bowtiePath);

    // print wall-clock time taken by search phases
    cmdLine.addArgument("-t");
    // eport end-to-end hits w/ <=v mismatches; ignore qualities
    cmdLine.addArgument("-v");
    cmdLine.addArgument("2");
    // number of alignment threads to launch
    cmdLine.addArgument("-p");
    cmdLine.addArgument("" + threads);
    // suppress all alignments if > <int> exist
    cmdLine.addArgument("-m");
    cmdLine.addArgument("10");
    // write hits in SAM format
    cmdLine.addArgument("-S");
    // bowtie index
    cmdLine.addArgument("${index}");
    if (read2 != null) {
        // fastq1
        cmdLine.addArgument("-1");
        cmdLine.addArgument("${read1}");
        // fastq2
        cmdLine.addArgument("-2");
        cmdLine.addArgument("${read2}");
    } else {//from   w ww. j  av a  2  s  . com
        cmdLine.addArgument("${read1}");
    }
    // output SAM file
    cmdLine.addArgument("${outFile}");

    Map map = new HashMap();
    map.put("index", index);
    map.put("read1", read1);
    if (read2 != null) {
        map.put("read2", read2);
    }
    map.put("outFile", outFile);
    cmdLine.setSubstitutionMap(map);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    // Never timeout 
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchdog);

    try {
        int exitValue = executor.execute(cmdLine);
    } catch (Exception e) {
        logger.fatal(
                "Failed to execute bowtie for aligning simulated Illumina reads from ART: " + e.getMessage());
    }
}

From source file:fr.gouv.culture.vitam.utils.Executor.java

/**
 * Execute an external command//from   w w  w  .  jav a 2 s  .  c  om
 * @param cmd
 * @param tempDelay
 * @param correctValues
 * @param showOutput
 * @param realCommand
 * @return correctValues if ok, < 0 if an execution error occurs, or other error values
 */
public static int exec(List<String> cmd, long tempDelay, int[] correctValues, boolean showOutput,
        String realCommand) {
    // Create command with parameters
    CommandLine commandLine = new CommandLine(cmd.get(0));
    for (int i = 1; i < cmd.size(); i++) {
        commandLine.addArgument(cmd.get(i));
    }
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    ByteArrayOutputStream outputStream;
    outputStream = new ByteArrayOutputStream();
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream);
    defaultExecutor.setStreamHandler(pumpStreamHandler);

    defaultExecutor.setExitValues(correctValues);
    AtomicBoolean isFinished = new AtomicBoolean(false);
    ExecuteWatchdog watchdog = null;
    Timer timer = null;
    if (tempDelay > 0) {
        // If delay (max time), then setup Watchdog
        timer = new Timer(true);
        watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
        defaultExecutor.setWatchdog(watchdog);
        CheckEndOfExecute endOfExecute = new CheckEndOfExecute(isFinished, watchdog, realCommand);
        timer.schedule(endOfExecute, tempDelay);
    }
    int status = -1;
    try {
        // Execute the command
        status = defaultExecutor.execute(commandLine);
    } catch (ExecuteException e) {
        if (e.getExitValue() == -559038737) {
            // Cannot run immediately so retry once
            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
            }
            try {
                status = defaultExecutor.execute(commandLine);
            } catch (ExecuteException e1) {
                pumpStreamHandler.stop();
                System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                        + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
                status = -2;
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                return status;
            } catch (IOException e1) {
                pumpStreamHandler.stop();
                System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                        + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
                status = -2;
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                return status;
            }
        } else {
            pumpStreamHandler.stop();
            System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                    + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
            status = -2;
            try {
                outputStream.close();
            } catch (IOException e2) {
            }
            return status;
        }
    } catch (IOException e) {
        pumpStreamHandler.stop();
        System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
        status = -2;
        try {
            outputStream.close();
        } catch (IOException e2) {
        }
        return status;
    } finally {
        isFinished.set(true);
        if (timer != null) {
            timer.cancel();
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e1) {
        }
    }
    pumpStreamHandler.stop();
    if (defaultExecutor.isFailure(status) && watchdog != null) {
        if (watchdog.killedProcess()) {
            // kill by the watchdoc (time out)
            if (showOutput) {
                System.err.println(StaticValues.LBL.error_error.get() + "Exec is in Time Out");
            }
        }
        status = -3;
        try {
            outputStream.close();
        } catch (IOException e2) {
        }
    } else {
        if (showOutput) {
            System.out.println("Exec: " + outputStream.toString());
        }
        try {
            outputStream.close();
        } catch (IOException e2) {
        }
    }
    return status;
}

From source file:com.adaptris.hpcc.SprayToThorTest.java

@Test
public void testLegacyCsvFormatMaxRecordSize() throws Exception {
    SprayToThor sprayToThor = new SprayToThor();
    sprayToThor.setFormat(SprayToThor.FORMAT.CSV);
    sprayToThor.setMaxRecordSize(214);//www  .  java  2s  .  c  om
    CommandLine cmdLine = new CommandLine("/bin/dfuplus");
    sprayToThor.addFormatArguments(cmdLine);
    assertEquals(2, cmdLine.getArguments().length);
    assertEquals("format=csv", cmdLine.getArguments()[0]);
    assertEquals("maxrecordsize=214", cmdLine.getArguments()[1]);
}

From source file:com.netflix.spinnaker.clouddriver.jobs.JobRequest.java

private CommandLine createCommandLine(List<String> tokenizedCommand) {
    if (tokenizedCommand == null || tokenizedCommand.size() == 0) {
        throw new IllegalArgumentException("No tokenizedCommand specified.");
    }/*from   w  w  w.  ja  va 2 s  . co m*/

    // Grab the first element as the command.
    CommandLine commandLine = new CommandLine(tokenizedCommand.get(0));

    int size = tokenizedCommand.size();
    String[] arguments = tokenizedCommand.subList(1, size).toArray(new String[size - 1]);
    commandLine.addArguments(arguments, false);
    return commandLine;
}

From source file:hu.bme.mit.trainbenchmark.sql.process.MySqlProcess.java

public static void runScript(final String scriptFile) throws ExecuteException, IOException {
    final Executor executor = new DefaultExecutor();
    final CommandLine commandLine = new CommandLine("/bin/bash");
    commandLine.addArgument(scriptFile, false);
    executor.execute(commandLine);// www  .j  a v a2  s .c o  m
}

From source file:io.covert.binary.analysis.ExecutorThread.java

public ExecutorThread(String prog, String[] args, Map<String, Object> substitutionMap, int[] exitCodes,
        long timeoutMS, File workingDirectory) throws ExecuteException, IOException {
    cmdLine = new CommandLine(prog);
    cmdLine.addArguments(args);//  w  ww  . jav  a 2  s. c o  m
    cmdLine.setSubstitutionMap(substitutionMap);

    resultHandler = new DefaultExecuteResultHandler();

    watchdog = new ExecuteWatchdog(60 * 1000);
    executor = new DefaultExecutor();

    stdOut = new ByteArrayOutputStream();
    stdErr = new ByteArrayOutputStream();

    streamHandler = new PumpStreamHandler(stdOut, stdErr);
    executor.setStreamHandler(streamHandler);
    executor.setWorkingDirectory(workingDirectory);
    executor.setExitValues(exitCodes);
    executor.setWatchdog(watchdog);
}