public static int execCommand(String... command) throws ExecuteException, IOException { CommandLine cmd = new CommandLine(command[0]); for (int i = 1; i < command.length; i++) { cmd.addArgument(command[i]); } DefaultExecutor exec = new DefaultExecutor(); return exec.execute(cmd); }
private Executor createExecutor(File workingDirectory, long timeoutInSeconds) { DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(workingDirectory); executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); // Fixes #41 if (timeoutInSeconds > 0) { executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000)); } return executor; }
String line = "myCommand.exe"; CommandLine commandLine = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(1); int exitValue = executor.execute(commandLine);
cmdLine = new CommandLine(cmdArray[0]); cmdLine.addArgument(cmdArray[i], false); cmdLine = CommandLine.parse(commandLine); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); Executor executor = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(stdout); executor.setExitValue(1); executor.setStreamHandler(streamHandler); executor.setWatchdog(watchdog); executor.execute(cmdLine, resultHandler); logger.debug("executed commandLine '{}'", commandLine); } catch (IOException e) { resultHandler.waitFor(); int exitCode = resultHandler.getExitValue(); retval = StringUtils.chomp(stdout.toString()); if (resultHandler.getException() != null) { logger.warn("{}", resultHandler.getException().getMessage()); } else { logger.debug("exit code '{}', result '{}'", exitCode, retval);
CommandLine cmd = new CommandLine(cmdlist[0]); for (String cmdItem : cmdlist) { if (!StringUtils.isBlank(cmdItem)) { cmd.addArgument(cmdItem); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); if (!StringUtils.isBlank(workDir)) { executor.setWorkingDirectory(new File(workDir)); PumpStreamHandler streamHandler = new PumpStreamHandler(out, out); executor.setStreamHandler(streamHandler); executor.execute(cmd, environment); } else { executor.execute(cmd, environment, resultHandler);
PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null); CommandLine commandline = new CommandLine(command); commandline.addArgument(scriptFile); if (params!=null && params.length>0) { commandline.addArguments(params); DefaultExecutor exec = new DefaultExecutor(); exec.setExitValues(null); exec.setStreamHandler(streamHandler); int exitValue = exec.execute(commandline); // exit code: 0=success, 1=error return exitValue; } catch (Exception e) {
private int executeSyncCommand(String command, String[] args, Map<Object, Object> newEnv, int timeout) throws MuleControllerException { CommandLine commandLine = new CommandLine(muleBin); commandLine.addArgument(command); commandLine.addArguments(args, false); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); executor.setStreamHandler(new PumpStreamHandler()); return doExecution(executor, commandLine, newEnv); }
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null); ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes); ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes); executor.setStreamHandler(new PumpStreamHandler(outStream, errStream)); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ExecBean res = new ExecBean(); res.exitcode = executor.execute(cmd, execEnv(env)); res.stderr = errStream.toString(enc); try { watchdog.checkException(); if(watchdog.killedProcess()) { String msg = " was terminated due to timeout(" + timeout + "ms). See " + AppConfig .EXEC_TIMEOUT_NAME + " property";
protected String executeExternalJavaProcess(Class<?> mainClass) throws IOException { String classpath = MvelTestUtils.getClassPath(); //See javadoc on MvelOverloadFailureReproduction for description of why we need to execute the test in a new JVM CommandLine cmdLine = new CommandLine("java"); cmdLine.addArgument("-cp"); cmdLine.addArgument(classpath, true); cmdLine.addArgument(mainClass.getName()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(baos)); try { executor.execute(cmdLine, new HashMap<String, String>()); } catch (IOException e) { throw new IOException(new String(baos.toByteArray())); } return new String(baos.toByteArray()); } }
import java.io.ByteArrayOutputStream; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.Executor; import org.apache.commons.exec.PumpStreamHandler; public String execToString(String command) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine commandline = CommandLine.parse(command); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); exec.execute(commandline); return(outputStream.toString()); }
private void executeScript(final ShardingContext shardingContext, final String scriptCommandLine) { CommandLine commandLine = CommandLine.parse(scriptCommandLine); commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false); try { new DefaultExecutor().execute(commandLine); } catch (final IOException ex) { throw new JobConfigurationException("Execute script failure.", ex); } } }
private CommandLine createCommandLine(List<String> command) { CommandLine commmandLine = new CommandLine(command.get(0)); for (int i = 1;i < command.size();i++) { String argument = command.get(i); commmandLine.addArgument(argument, false); } return commmandLine; }
private int execute(final Logger logger, final OutputStream stdout, final OutputStream stderr) throws ProcessExecutionException { logger.debug("Executing command line {}", commandLine); try { ExecuteStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(commandLine, environment); logger.debug("Exit value {}", exitValue); return exitValue; } catch (ExecuteException e) { if (executor.getWatchdog() != null && executor.getWatchdog().killedProcess()) { throw new ProcessExecutionException("Process killed after timeout"); } throw new ProcessExecutionException(e); } catch (IOException e) { throw new ProcessExecutionException(e); } }
/** * @see org.apache.commons.exec.Executor#execute(CommandLine, * org.apache.commons.exec.ExecuteResultHandler) */ public void execute(final CommandLine command, final ExecuteResultHandler handler) throws ExecuteException, IOException { execute(command, null, handler); }
/** * Add multiple arguments. Handles parsing of quotes and whitespace. * Please note that the parsing can have undesired side-effects therefore * it is recommended to build the command line incrementally. * * @param addArguments An string containing multiple arguments. * @return The command line itself */ public CommandLine addArguments(final String addArguments) { return this.addArguments(addArguments, true); }
public void run() { int exitValue = Executor.INVALID_EXITVALUE; try { exitValue = executeInternal(command, environment, workingDirectory, streamHandler); handler.onProcessComplete(exitValue); } catch (final ExecuteException e) { handler.onProcessFailed(e); } catch (final Exception e) { handler.onProcessFailed(new ExecuteException("Execution failed", exitValue, e)); } } };
/** * Logs a line to the log system of the user. * * @param line * the line to log. */ protected void processLine(final String line) { processLine(line, level); }
CommandLine cmd = new CommandLine(cmdlist[0]); for (String cmdItem : cmdlist) { if (StringUtils.isBlank(cmdItem) == false) { cmd.addArgument(cmdItem); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); if (StringUtils.isBlank(workDir) == false) { executor.setWorkingDirectory(new File(workDir)); PumpStreamHandler streamHandler = new PumpStreamHandler(out, out); if (streamHandler != null) { executor.setStreamHandler(streamHandler); executor.execute(cmd, environment); } else { executor.execute(cmd, environment, resultHandler);
public static void exec_command(String command) throws ExecuteException, IOException { String[] cmdlist = command.split(" "); CommandLine cmd = new CommandLine(cmdlist[0]); for (int i = 1; i < cmdlist.length; i++) { cmd.addArgument(cmdlist[i]); } DefaultExecutor exec = new DefaultExecutor(); exec.execute(cmd); }
private CommandLine makeCommandLine(String program, List<String> args) throws NotAuthorizedException, IOException { String path = validateProgram(program); CommandLine cmd = new CommandLine(path); if (args != null) for (String arg : args) cmd.addArgument(arg, false); return cmd; }