本文整理了Java中org.apache.commons.exec.Executor
类的一些代码示例,展示了Executor
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Executor
类的具体详情如下:
包路径:org.apache.commons.exec.Executor
类名称:Executor
[英]The main abstraction to start an external process. The interface allows to
Executor exec = new DefaultExecutor();
CommandLine cl = new CommandLine("ls -l");
int exitvalue = exec.execute(cl);
[中]启动外部流程的主要抽象。该接口允许
*为子流程设置当前工作目录
*提供一组传递给子流程的环境变量
*使用ExecuteStreamHandler捕获stdout和stderr的子流程输出
*使用ExecuteWatchLog终止长时间运行的进程
*定义一组预期的退出值
*当主进程使用ProcessDestroyer终止时,终止任何已启动的进程
以下示例显示了基本用法:
Executor exec = new DefaultExecutor();
CommandLine cl = new CommandLine("ls -l");
int exitvalue = exec.execute(cl);
代码示例来源:origin: openhab/openhab1-addons
cmdLine = new CommandLine(cmdArray[0]);
cmdLine.addArgument(cmdArray[i], false);
cmdLine = CommandLine.parse(commandLine);
Executor executor = new DefaultExecutor();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
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());
代码示例来源:origin: eirslett/frontend-maven-plugin
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);
}
}
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
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());
}
}
代码示例来源:origin: alexo/wro4j
/**
* @VisibleForTesting
*/
void doProcess(final InputStream in, final OutputStream out)
throws ExecuteException, IOException {
final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
final Executor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(out, errorStream, in));
final int result = executor.execute(CommandLine.parse(NGMIN_COMMAND));
LOG.debug("result={}", result);
if (result != 0) {
throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result);
}
}
代码示例来源:origin: com.hurence.logisland/logisland-agent
CommandLine cmdLine = new CommandLine("yarn");
cmdLine.addArgument("application");
cmdLine.addArgument("-list");
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
Executor executor = new DefaultExecutor();
executor.setExitValue(0);
executor.setStreamHandler(psh);
try {
executor.execute(cmdLine);
} catch (IOException e) {
logger.error(e.toString());
YarnApplicationWrapper wrapper = new YarnApplicationWrapper(stdout.toString());
YarnApplication app = wrapper.getApplication(job.getName());
if (app != null) {
killCmdLine.addArgument("-kill");
killCmdLine.addArgument(app.getId());
Executor killExecutor = new DefaultExecutor();
killExecutor.setExitValue(0);
try {
killExecutor.execute(killCmdLine);
} catch (IOException e) {
logger.error(e.toString());
代码示例来源:origin: nidi3/graphviz-java
public void execute(CommandLine cmd, @Nullable File workingDirectory) throws InterruptedException, IOException {
LOG.info("executing command {}", cmd.toString());
final ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);
final Executor executor = new org.apache.commons.exec.DefaultExecutor();
executor.setWatchdog(watchdog);
if (workingDirectory != null) {
executor.setWorkingDirectory(workingDirectory);
}
LOG.debug("workdir: {}", executor.getWorkingDirectory());
final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ByteArrayOutputStream err = new ByteArrayOutputStream();
executor.setStreamHandler(new PumpStreamHandler(out, err));
executor.execute(cmd, resultHandler);
resultHandler.waitFor();
final int exitCode = resultHandler.getExitValue();
if (out.size() > 0) {
LOG.info(out.toString());
}
if (exitCode != 0) {
throw new IOException(err.size() == 0 ? "command '" + cmd + "' didn't succeed" : err.toString());
}
}
}
代码示例来源:origin: alexholmes/hdfs-file-slurper
CommandLine commandLine = new CommandLine(execAndArgs[0]);
commandLine.addArguments(Arrays.copyOfRange(execAndArgs, 1, execAndArgs.length));
Executor executor = new DefaultExecutor();
executor.setExitValue(0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayOutputStream baes = new ByteArrayOutputStream();
ByteArrayInputStream bais = new ByteArrayInputStream((stdInLine + "\n").getBytes());
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(baos, baes, bais);
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);
executor.execute(commandLine);
} catch (IOException e) {
log.error("Script exited with non-zero exit code");
log.error("Stdout = ");
log.error(baos.toString());
log.error("Stderr = ");
log.error(baes.toString());
代码示例来源:origin: ga4gh/dockstore
try (ByteArrayOutputStream localStdoutStream = new ByteArrayOutputStream();
ByteArrayOutputStream localStdErrStream = new ByteArrayOutputStream()) {
OutputStream stdout = localStdoutStream;
OutputStream stderr = localStdErrStream;
String utf8 = StandardCharsets.UTF_8.name();
try {
final CommandLine parse = CommandLine.parse(command);
Executor executor = new DefaultExecutor();
if (workingDir != null) {
LOG.info("working directory is " + workingDir.toString());
executor.setWorkingDirectory(workingDir);
executor.setExitValue(0);
if (dumpOutput) {
LOG.info("CMD: " + command);
executor.setStreamHandler(new PumpStreamHandler(stdout, stderr));
executor.execute(parse, resultHandler);
resultHandler.waitFor();
throw new ExecuteException("problems running command: " + command, resultHandler.getExitValue());
return new ImmutablePair<>(localStdoutStream.toString(utf8), localStdErrStream.toString(utf8));
} catch (InterruptedException | IOException e) {
throw new RuntimeException("problems running command: " + command, e);
代码示例来源:origin: apache/bigtop
System.out.println(commandline.toString());
Map<String, String> env = null;
Map<String, String> entry = new HashMap<String, String>();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 10000);
Executor executor = new DefaultExecutor();
executor.setExitValue(1);
executor.setWatchdog(watchdog);
executor.setStreamHandler(streamHandler);
try {
executor.execute(commandline, env, resultHandler);
} catch (ExecuteException e) {
LOG.debug("outputStream: " + outputStream.toString());
entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
entry.put("outputStream", outputStream.toString() + e.getMessage());
e.printStackTrace();
return entry;
代码示例来源:origin: com.hurence.logisland/logisland-agent
CommandLine cmdLine = new CommandLine("bin/logisland-launch-spark-job");
cmdLine.addArgument("--agent");
cmdLine.addArgument("http://0.0.0.0:8081");
cmdLine.addArgument("--job");
cmdLine.addArgument(jobId);
executor.setWatchdog(watchdog);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
executor.setExitValue(0);
executor.setStreamHandler(psh);
try {
executor.execute(cmdLine, resultHandler);
} catch (IOException e) {
e.printStackTrace();
代码示例来源:origin: org.apache.stanbol/org.apache.stanbol.commons.testing.jarexec
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) {
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);
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
try {
logger.trace("Executing: " + fullCommand);
CommandLine cli = CommandLine.parse(fullCommand);
Executor executor = new DefaultExecutor();
LogOutputStream outStream =
new LogOutputStream() {
closer.register(outStream);
closer.register(errStream);
ExecuteStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
executor.setStreamHandler(streamHandler);
executor.setWatchdog(watchDog);
int retValue = executor.execute(cli, ENVIRONMENT_MAP);
if (retValue != 0) {
logger.error(
代码示例来源:origin: Findwise/Hydra
public void printJavaVersion() {
CommandLine cmdLine = new CommandLine("java");
cmdLine.addArgument("-version");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
Executor executor = new DefaultExecutor();
try {
executor.execute(cmdLine, resultHandler);
} catch (ExecuteException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: com.googlecode.etl-unit/etlunit-core
final CommandLine cmdLine = new CommandLine(pd.getCommand());
cmdLine.addArgument(it.next());
Executor executor = new DefaultExecutor();
new ModifiedPumpStreamHandler(pd.getOutputFile());
executor.setStreamHandler(modifiedPumpStreamHandler);
executor.setWatchdog(watchdog);
executor.setWorkingDirectory(pd.getWorkingDirectory());
executor.execute(cmdLine, getenv, resultHandler);
代码示例来源:origin: gov.nasa.jpl.imce/gov.nasa.jpl.magicdraw.projectUsageIntegrityChecker
CommandLine cmdLine = new CommandLine(dotCommand);
cmdLine.addArgument("-Tpng");
cmdLine.addArgument("-o");
cmdLine.addArgument(pugImage.getName());
cmdLine.addArgument(pugDOT.getName());
Executor executor = new DefaultExecutor();
executor.setExitValue(0);
executor.setWatchdog(watchdog);
executor.setWorkingDirectory(pugTemp);
executor.execute(cmdLine, resultHandler);
if (!executor.isFailure(resultHandler.getExitValue())) {
pluginLog.info(String.format("%s - convertDOTgraph - reading image for '%s' from: '%s'", pluginName, project.getName(), pugImage.getName()));
BufferedImageFile imageFile = new BufferedImageFile(pugImage);
代码示例来源:origin: com.netflix.genie/genie-core
private void makeDirGroupWritable(final String dir) throws GenieServerException {
log.debug("Adding write permissions for the directory {} for the group.", dir);
final CommandLine commandLIne = new CommandLine("sudo").addArgument("chmod").addArgument("g+w")
.addArgument(dir);
try {
this.executor.execute(commandLIne);
} catch (IOException ioe) {
throw new GenieServerException("Could not make the job working logs directory group writable.", ioe);
}
}
代码示例来源:origin: com.github.becausetesting/commons
/**
* Execute a command on the operating system using Apache Commons Exec. This
* function runs asynchronously and dumps both stderr and stdout streams to
* a temp file.
*
* @param commandLine
* The command to be executed.
* @param outputStreamHandler
* An output stream to dump the process stderr and stdout to it.
*/
public static void runCommandUsingApacheExec(CommandLine commandLine, OutputStream outputStreamHandler) {
try {
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStreamHandler);
logger.info("commandLine: " + commandLine.toString());
Executor process = new DefaultExecutor();
process.setExitValue(0);
process.setStreamHandler(streamHandler);
process.execute(commandLine, resultHandler);
// resultHandler.waitFor();
} catch (Exception ex) {
logger.error("An exception was thrown.", ex);
}
}
代码示例来源:origin: org.scala-tools/maven-scala-plugin
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
List<String> cmd = buildCommand();
displayCmd(displayCmd, cmd);
Executor exec = new DefaultExecutor();
exec.setStreamHandler(new PumpStreamHandler(System.out));
} else {
exec.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
CommandLine cl = new CommandLine(cmd.get(0));
for (int i = 1; i < cmd.size(); i++) {
cl.addArgument(cmd.get(i));
int exitValue = exec.execute(cl);
if (exitValue != 0) {
if (throwFailure) {
代码示例来源:origin: com.hotels/shunting-yard-replicator
public void run(Context context) {
try (OutputStream out = outStream(context); OutputStream err = errStream(context)) {
CommandLine cli = CommandLine
.parse(String.format("%s/%s", getProcEnvironment().get(CIRCUS_TRAIN_HOME_ENV_VAR), CIRCUS_TRAIN_HOME_SCRIPT));
cli.addArgument("--config=" + context.getConfigLocation());
if (!StringUtils.isEmpty(context.getCircusTrainConfigLocation())) {
cli.addArgument("--config=" + context.getCircusTrainConfigLocation());
}
Executor executor = new DefaultExecutor();
executor.setWorkingDirectory(new File(context.getWorkspace()));
executor.setStreamHandler(new PumpStreamHandler(out, err));
log.debug("Executing {} with environment {}", cli, getProcEnvironment());
int returnValue = executor.execute(cli, getProcEnvironment());
log.debug("Command exited with value {} ", returnValue);
if (returnValue != 0) {
throw new CircusTrainException("Circus Train exited with error value " + returnValue);
}
} catch (Throwable e) {
log.error("Unable to execute Circus Train", e);
}
}
代码示例来源:origin: org.seleniumhq.selenium/selenium-iphone-driver
private static Executor getOutputIgnoringExecutor() {
Executor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(null, null));
return executor;
}
}
内容来源于网络,如有侵权,请联系作者删除!