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

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

Introduction

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

Prototype

public CommandLine addArguments(final String addArguments, final boolean handleQuoting) 

Source Link

Document

Add multiple arguments.

Usage

From source file:generator.Utils.java

public static boolean exec(String command, File workingDir, String... args) {
    try {//from   w ww.  j a  va  2  s .  co m
        CommandLine cmdLine = new CommandLine(command);
        cmdLine.addArguments(args, false);
        DefaultExecutor exe = new DefaultExecutor();
        exe.setWorkingDirectory(workingDir);
        return exe.execute(cmdLine) == 0;
    } catch (Exception e) {
        return false;
    }
}

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  ww  w .j ava  2 s . 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:latexstudio.editor.runtime.CommandLineExecutor.java

public static synchronized void executeGeneratePDF(CommandLineBuilder cmd) {
    String outputDirectory = "--output-directory=" + cmd.getOutputDirectory();
    String outputFormat = "--output-format=pdf";
    String job = cmd.getJobname() == null ? "" : "--jobname=" + cmd.getJobname().replaceAll(" ", "_");
    ByteArrayOutputStream outputStream = null;

    try {//from w w w .  ja  v  a 2 s  . c om
        String[] command = new String[] { outputDirectory, outputFormat, job, cmd.getPathToSource() };

        CommandLine cmdLine = new CommandLine(ApplicationUtils.getPathToTEX(cmd.getLatexPath()));
        //For windows, we set handling quoting to true
        cmdLine.addArguments(command, ApplicationUtils.isWindows());

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

        if (cmd.getWorkingFile() != null) {
            EXECUTOR.setWorkingDirectory(cmd.getWorkingFile().getParentFile().getAbsoluteFile());
        }

        EXECUTOR.execute(cmdLine);

        if (cmd.getLogger() != null) {
            cmd.getLogger().log(cmdLine.toString());
            cmd.getLogger().log(outputStream.toString());
        }

    } catch (IOException e) {
        if (cmd.getLogger() != null) {
            cmd.getLogger().log("The path to the pdflatex tool is incorrect or has not been set.");
        }
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

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);//from   w  ww .  ja v  a  2s .c  o  m
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}

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

public void execute() throws MojoExecutionException {

    if (isSkipExecution()) {
        getLog().info("Test phase skipped");
        return;//from   w w w. jav a2s  .  c o m
    }

    String line = getPlay2().getAbsolutePath();

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

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

    executor.setWorkingDirectory(project.getBasedir());

    executor.setExitValue(0);
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        if (testFailureIgnore) {
            getLog().error("Test execution failures ignored");
        } else {
            throw new MojoExecutionException("Error during compilation", e);
        }
    }
}

From source file:com.sonar.scanner.api.it.tools.CommandExecutor.java

public int execute(String[] args, Map<String, String> env, @Nullable Path workingDir) throws IOException {
    if (!Files.isExecutable(file)) {
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        Files.setPosixFilePermissions(file, perms);
    }//from ww  w .j a va  2s .  c om

    watchdog = new ExecuteWatchdog(TIMEOUT);
    CommandLine cmd = new CommandLine(file.toFile());
    cmd.addArguments(args, false);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(watchdog);
    exec.setStreamHandler(createStreamHandler());
    exec.setExitValues(null);
    if (workingDir != null) {
        exec.setWorkingDirectory(workingDir.toFile());
    }
    in.close();
    LOG.info("Executing: {}", cmd.toString());
    return exec.execute(cmd, env);
}

From source file:com.demandware.vulnapp.challenge.impl.CommandInjectionChallenge.java

private String getCommandOutput(String command) {
    String output = null;//  w ww  .ja v a  2 s .c o  m
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        CommandLine cmd = new CommandLine("/bin/bash");
        String[] args = new String[] { "-c", command };
        cmd.addArguments(args, false);
        PumpStreamHandler psh = new PumpStreamHandler(outputStream);

        DefaultExecutor exec = new DefaultExecutor();
        exec.setStreamHandler(psh);
        exec.execute(cmd);
        output = outputStream.toString();
    } catch (ExecuteException e) {
        e.printStackTrace();
        output = "Could not execute command";
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}

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  .j  a  v  a2 s.  c om*/

    // 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:com.blackducksoftware.tools.scmconnector.integrations.teamfoundation.TeamFoundationConnector.java

@Override
public int sync() {

    workspaceExists();/*from  w ww .j av  a2  s.  c o  m*/

    if (!workspaceExists) {

        if (createWorkpace() != 0) {
            return 1;
        }

        if (mapWorkpace() != 0) {
            return 1;
        }
    }

    CommandLine command = CommandLine.parse(executable);

    String[] arguments = new String[] { "get", "-recursive", "-login:" + user + "," + password,
            getFinalSourceDirectory() };
    command.addArguments(arguments, false);

    int exitStatus = 1;

    try {
        exitStatus = commandLineExecutor.executeCommand(log, command, new File(getFinalSourceDirectory()));
    } catch (Exception e) {
        log.error("Failure executing TF Command", e);
    }

    return exitStatus;

}

From source file:com.blackducksoftware.tools.scmconnector.integrations.teamfoundation.TeamFoundationConnector.java

private int createWorkpace() {
    CommandLine command = CommandLine.parse(executable);

    String[] arguments = new String[] { "workspace", "-new", "-collection:" + server + "/" + collection,
            "-login:" + user + "," + password, workspace };

    command.addArguments(arguments, false);

    int exitStatus = 1;

    try {/*  w w  w  .  jav a 2 s .  co  m*/
        exitStatus = commandLineExecutor.executeCommand(log, command, new File(getFinalSourceDirectory()));

    } catch (Exception e) {

        log.error("Failure executing TF Command", e);
    }

    return exitStatus;
}