Package org.apache.commons.exec

Examples of org.apache.commons.exec.DefaultExecutor


        }
       
        cmd.addArgument(gwtModuleName);
               
        try {
            DefaultExecutor executor = new DefaultExecutor();

            getLog().debug("Exec: " + cmd.toString());

            int ret = executor.execute(cmd, CommandLineUtils.getSystemEnvVars());
            if (ret != 0) {
                throw new MojoExecutionException("Exec failed: " + Integer.toString(ret));
            }
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage());
View Full Code Here


  private static String execute(String commandLine) throws ExecuteException, IOException {
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    CommandLine cl = CommandLine.parse("/bin/bash " + commandLine);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWorkingDirectory(PROJECT_BASE_DIR);
    exec.setStreamHandler(psh);
    try {
      exec.execute(cl, env);
    } catch (ExecuteException e) {
      System.out.println(stdout.toString());
      throw e;
    } catch (IOException e) {
      System.out.println(stdout.toString());
View Full Code Here

    if(StringUtils.isBlank(execCommand)){
      warn(out, "No command need execute.");
      return;
    }
   
    Executor executor = new DefaultExecutor();
        executor.setWorkingDirectory(site.getParentFile());

        CommandLine command = CommandLine.parse(execCommand);
       
        out.println("========================");
        info(out, String.format("Execute command: %s",  command.toString()));
        out.println("========================");
        LogOutputStream outputStream = new LogOutputStream() {
            protected void processLine(String line, int level){
                log.info(String.format("Command logged an out: %s", line));
                out.println(line);
            }
        };
        LogOutputStream errorStream = new LogOutputStream() {
            protected void processLine(String line, int level){
                log.error(String.format("Command logged an error: %s", line));
                out.println(line);
            }
        };
       
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
        executor.setStreamHandler(streamHandler);
        executor.setProcessDestroyer(processDestroyer);
       
//        (new Thread(new Runnable(){
//            public void run(){
//                try{
//                    executor.execute(command);
//                }
//                catch(Exception e) {
//                    log.warn(String.format("Command exited with error %s", e.getMessage()));
//                }
//            }
//        })).start();
 
    executor.execute(command);
  }
View Full Code Here

     * @return a reader containing the output of the process
     * @throws IOException starting the process failed
     */
    protected BufferedReader runProcEnvCommand() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Executor exe = new DefaultExecutor();
        exe.setStreamHandler(new PumpStreamHandler(out));
        // ignore the exit value - Just try to use what we got
        exe.execute(getProcEnvCommand());
        return new BufferedReader(new StringReader(toString(out)));
    }
View Full Code Here

        notNull(command, "command");

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();

        DefaultExecutor executor = prepareDefaultExecutor(command);
        // handle error and output of the process and write them to the given
        // out stream
        PumpStreamHandler handler = new PumpStreamHandler(out, err, command.getInput());
        executor.setStreamHandler(handler);

        CommandLine cl = toCommandLine(command);

        try {
            int exitValue = executor.execute(cl);
            // if the size is zero, we have no output, so construct the result
            // with null (required by ExecResult)
            InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
            InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
            ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
View Full Code Here

            IOUtils.closeQuietly(command.getInput());
        }
    }

    protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(null);

        if (execCommand.getWorkingDir() != null) {
            executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile());
        }
        if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) {
            executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout()));
        }
        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
        return executor;
    }
View Full Code Here

     * @return a reader containing the output of the process
     * @throws IOException starting the process failed
     */
    protected BufferedReader runProcEnvCommand() throws IOException {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final Executor exe = new DefaultExecutor();
        exe.setStreamHandler(new PumpStreamHandler(out));
        // ignore the exit value - Just try to use what we got
        exe.execute(getProcEnvCommand());
        return new BufferedReader(new StringReader(toString(out)));
    }
View Full Code Here

            cl.addArguments(clojureArgs, false);
        }

        getLog().debug("Command line: " + cl.toString());

        Executor exec = new DefaultExecutor();
        Map<String, String> env = new HashMap<String, String>(System.getenv());
//        env.put("path", ";");
//        env.put("path", System.getProperty("java.home"));

        ExecuteStreamHandler handler = new PumpStreamHandler(System.out, System.err, System.in);
        exec.setStreamHandler(handler);
        exec.setWorkingDirectory(getWorkingDirectory());

        int status;
        try {
            status = exec.execute(cl, env);
        } catch (ExecuteException e) {
            status = e.getExitValue();
        } catch (IOException e) {
            status = 1;
        }
View Full Code Here

                log.info("Process execution complete, exit code=" + result);
            }
        };

        final String vmOptions = System.getProperty("jar.executor.vm.options");
        final Executor e = new DefaultExecutor();
        if (this.workingDirectory != null) {
            e.setWorkingDirectory(this.workingDirectory);
        }
        final CommandLine cl = new CommandLine(javaExecutable);
        if (vmOptions != null && vmOptions.length() > 0) {
            // TODO: this will fail if one of the vm options as a quoted value with a space in it, but this is
            // not the case for common usage patterns
            for (String option : StringUtils.split(vmOptions, " ")) {
                cl.addArgument(option);
            }
        }
        cl.addArgument("-jar");
        cl.addArgument(jarToExecute.getAbsolutePath());
        cl.addArgument("-p");
        cl.addArgument(String.valueOf(serverPort));
        log.info("Executing " + cl);
        e.setStreamHandler(new PumpStreamHandler());
        e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
        e.execute(cl, h);
    }
View Full Code Here

          if ( arguments != null ) {
            line += " " + arguments;
          }

          CommandLine commandLine = CommandLine.parse(line);
          DefaultExecutor executor = new DefaultExecutor();
          int exitValue = executor.execute(commandLine);
         
          if ( exitValue != 0 ) {
              throw new MojoExecutionException( "Problem executing light, return code " + exitValue );
          }
        
View Full Code Here

TOP

Related Classes of org.apache.commons.exec.DefaultExecutor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.