本文整理了Java中org.apache.commons.exec.Executor.execute()
方法的一些代码示例,展示了Executor.execute()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Executor.execute()
方法的具体详情如下:
包路径:org.apache.commons.exec.Executor
类名称:Executor
方法名:execute
[英]Methods for starting synchronous execution. The child process inherits all environment variables of the parent process.
[中]用于启动同步执行的方法。子进程继承父进程的所有环境变量。
代码示例来源: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: openhab/openhab1-addons
executor.execute(cmdLine, resultHandler);
logger.debug("executed commandLine '{}'", commandLine);
} catch (IOException 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: com.datastax.cassandra/cassandra-driver-core
executor.setStreamHandler(streamHandler);
executor.setWatchdog(watchDog);
int retValue = executor.execute(cli, ENVIRONMENT_MAP);
if (retValue != 0) {
logger.error(
代码示例来源:origin: com.netflix.genie/genie-core
/**
* {@inheritDoc}
*/
@Override
public void checkProcess() throws GenieTimeoutException, ExecuteException, IOException {
this.executor.execute(this.commandLine);
// If we get here the process is still running. Check if it should be killed due to timeout.
if (new Date().getTime() > this.timeout.getTime()) {
throw new GenieTimeoutException(
"Job has exceeded its timeout time of " + this.dateFormatter.format(this.timeout)
);
}
}
}
代码示例来源:origin: danielflower/app-runner
public static Pair<Boolean, String> run(CommandLine command) {
ExecuteWatchdog watchDog = new ExecuteWatchdog(30000);
StringBuffer output = new StringBuffer();
Executor executor = createExecutor(output::append, command, new File("."), watchDog);
output.setLength(0);
try {
int exitValue = executor.execute(command);
if (executor.isFailure(exitValue)) {
return Pair.of(false, "Not available");
}
} catch (Exception e) {
return Pair.of(false, "Not available");
}
return Pair.of(true, output.toString().trim());
}
代码示例来源: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: ro.isdc.wro4j/wro4j-extensions
/**
* @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: ro.isdc.wro4j/wro4j-extensions
/**
* @VisibleForTesting
*/
void doProcess(final InputStream in, final OutputStream out)
throws 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(NGANN_COMMAND));
LOG.debug("result={}", result);
if (result != 0) {
throw new ExecuteException("Processing failed: " + new String(errorStream.toByteArray()), result);
}
}
代码示例来源: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: org.seleniumhq.selenium/selenium-iphone-driver
public void launch() {
Executor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(createOutputStream()));
try {
exitCode = executor.execute(commandLine);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: com.addc.mojo/addc-mojo
/**
* Execute the sub-process
*
* @param executor
* The executor to use
* @param cmdLine
* The command line to execute
* @param streamHandler
* The stream handler to use
* @throws MojoExecutionException
* If the process execution fails
*/
protected void executeTarget(Executor executor, CommandLine cmdLine, PumpStreamHandler streamHandler)
throws MojoExecutionException {
executor.setStreamHandler(streamHandler);
try {
executor.execute(cmdLine);
} catch (IOException e) {
getLog().error("Failed to run " + cmdLine.getExecutable(), e);
throw new MojoExecutionException("Failed to run " + cmdLine.getExecutable(), e);
}
}
}
代码示例来源: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: com.github.becauseQA/becauseQA-utils
/**
* 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: com.datastax.dse/dse-java-driver-core
/**
* Executes the given command with KRB5_CONFIG environment variable pointing to the specialized
* config file for the embedded KDC server.
*/
public static void executeCommand(String command, EmbeddedADS adsServer) throws IOException {
Map<String, String> environmentMap =
ImmutableMap.<String, String>builder()
.put("KRB5_CONFIG", adsServer.getKrb5Conf().getAbsolutePath())
.build();
CommandLine cli = CommandLine.parse(command);
Executor executor = new DefaultExecutor();
int retValue = executor.execute(cli, environmentMap);
assertThat(retValue).isZero();
}
代码示例来源: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: net.jangaroo/jangaroo-maven-plugin
private static void internalExecute(CommandLine cmdLine, OutputStream outputStream, File workingDirectory) throws IOException {
Executor executor = getExecutor();
ExecuteWatchdog watchdog = getExecuteWatchdog();
executor.setWatchdog(watchdog);
if (workingDirectory != null) {
executor.setWorkingDirectory(workingDirectory);
}
// set allowed exit values (0 is actually the default)
executor.setExitValue(0);
PumpStreamHandler psh = new PumpStreamHandler(outputStream);
executor.setStreamHandler(psh);
executor.execute(cmdLine);
if (watchdog.killedProcess()) {
throw new ExecuteException(String.format("Watchdog killed Sencha Cmd process after %s ms.", MAX_EXECUTION_TIME), 0);
}
}
代码示例来源:origin: com.netflix.genie/genie-core
/**
* Method to change the ownership of a directory.
*
* @param dir The directory to change the ownership of.
* @param user Userid of the user.
* @throws GenieException If there is a problem.
*/
protected void changeOwnershipOfDirectory(
final String dir,
final String user) throws GenieException {
final CommandLine commandLine = new CommandLine("sudo").addArgument("chown").addArgument("-R")
.addArgument(user).addArgument(dir);
try {
this.executor.execute(commandLine);
} catch (IOException ioexception) {
throw new GenieServerException("Could not change ownership", ioexception);
}
}
代码示例来源:origin: danielflower/app-runner
public static void run(LineConsumer outputHandler, Map<String, String> envVarsForApp, CommandLine command, File projectRoot, long timeout) throws ProjectCannotStartException {
long startTime = logStartInfo(command, projectRoot);
ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout);
Executor executor = createExecutor(outputHandler, command, projectRoot, watchDog);
try {
int exitValue = executor.execute(command, envVarsForApp);
if (executor.isFailure(exitValue)) {
String message = watchDog.killedProcess()
? "Timed out waiting for " + command
: "Exit code " + exitValue + " returned from " + command;
throw new ProjectCannotStartException(message);
}
} catch (Exception e) {
String message = "Error running: " + fullPath(projectRoot) + "> " + StringUtils.join(command.toStrings(), " ");
outputHandler.consumeLine(message);
outputHandler.consumeLine(e.toString());
throw new ProjectCannotStartException(message, e);
}
logEndTime(command, startTime);
}
内容来源于网络,如有侵权,请联系作者删除!