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) 

Source Link

Document

Add multiple arguments.

Usage

From source file:org.sonatype.sisu.bl.support.DefaultDropwizardBundle.java

@Override
protected void startApplication() {
    File bundleDirectory = getBundleDirectory();
    List<String> javaOptions = getConfiguration().getJavaOptions();
    List<String> javaAgentOptions = getJavaAgentOptions();

    CommandLine cmdLine = new CommandLine(new File(System.getProperty("java.home"), "/bin/java"));
    if (javaAgentOptions.size() > 0) {
        cmdLine.addArguments(javaAgentOptions.toArray(new String[javaAgentOptions.size()]));
    }//from w w  w  .  java2 s  .c  om
    if (javaOptions.size() > 0) {
        cmdLine.addArguments(javaOptions.toArray(new String[javaOptions.size()]));
    }
    cmdLine.addArgument("-jar").addArgument(getJarName()).addArguments(getConfiguration().arguments())
            .addArgument("config.yaml");

    log.debug("Launching: {}", cmdLine.toString());

    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(bundleDirectory);
    executor.setWatchdog(watchdog = new ExecuteWatchdog(Time.minutes(5).toMillis()));

    try {
        executor.setStreamHandler(streamHandler = new PumpStreamHandler(
                new FileOutputStream(new File(bundleDirectory, "output.log"))));
        executor.execute(cmdLine, new DefaultExecuteResultHandler());
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.sourcepit.common.maven.testing.ExternalMavenTest.java

protected CommandLine newCmd(File binDir, String bat, String sh, String... arguments) {
    final CommandLine cmd;
    if (OS.isFamilyWindows() || OS.isFamilyWin9x()) {
        cmd = process.newCommandLine(new File(binDir, bat));
    } else if (OS.isFamilyUnix() || OS.isFamilyMac()) {
        cmd = process.newCommandLine("sh", new File(binDir, sh).getAbsolutePath());
    } else {/*  w ww.j ava2 s  . c  o  m*/
        throw new AssertionFailedError("Os family");
    }
    cmd.addArguments(arguments);
    return cmd;
}

From source file:pl.net.ptak.InstallShieldBuildMojo.java

private void addCmdLnArguments(CommandLine cl, String... params) {
    cl.addArguments(params);
}

From source file:sce.ProcessExecutor.java

public String executeProcess(String[] processParameters) throws JobExecutionException {
    try {/*from  w  w w . j a  v  a 2s  .com*/
        //Command to be executed
        CommandLine command = new CommandLine(processParameters[0]);

        String[] params = new String[processParameters.length - 1];
        for (int i = 0; i < processParameters.length - 1; i++) {
            params[i] = processParameters[i + 1];
        }

        //Adding its arguments
        command.addArguments(params);

        //set timeout in seconds
        ExecuteWatchdog watchDog = new ExecuteWatchdog(
                this.timeout == 0 ? ExecuteWatchdog.INFINITE_TIMEOUT : this.timeout * 1000);
        this.watchdog = watchDog;

        //Result Handler for executing the process in a Asynch way
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        //MyResultHandler resultHandler = new MyResultHandler();

        //Using Std out for the output/error stream
        //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        //This is used to end the process when the JVM exits
        ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();

        //Our main command executor
        DefaultExecutor executor = new DefaultExecutor();

        //Setting the properties
        executor.setStreamHandler(new PumpStreamHandler(null, null));
        executor.setWatchdog(watchDog);
        //executor.setExitValue(1); // this has to be set if the java code contains System.exit(1) to avoid a FAILED status

        //Setting the working directory
        //Use of recursion along with the ls makes this a long running process
        //executor.setWorkingDirectory(new File("/home"));
        executor.setProcessDestroyer(processDestroyer);

        //if set, use the java environment variables when running the command
        if (!this.environment.equals("")) {
            Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(procEnv, this.environment);
            //Executing the command
            executor.execute(command, procEnv, resultHandler);
        } else {
            //Executing the command
            executor.execute(command, resultHandler);
        }

        //The below section depends on your need
        //Anything after this will be executed only when the command completes the execution
        resultHandler.waitFor();

        /*int exitValue = resultHandler.getExitValue();
         System.out.println(exitValue);
         if (executor.isFailure(exitValue)) {
         System.out.println("Execution failed");
         } else {
         System.out.println("Execution Successful");
         }
         System.out.println(outputStream.toString());*/
        //return outputStream.toString();
        if (watchdog.killedProcess()) {
            throw new JobExecutionException("Job Interrupted", new InterruptedException());
        }
        if (executor.isFailure(resultHandler.getExitValue())) {
            ExecuteException ex = resultHandler.getException();
            throw new JobExecutionException(ex.getMessage(), ex);
        }
        return "1";
    } catch (ExecuteException ex) {
        throw new JobExecutionException(ex.getMessage(), ex);
    } catch (IOException | InterruptedException | JobExecutionException ex) {
        throw new JobExecutionException(ex.getMessage(), ex);
    }
}