本文整理汇总了Java中org.apache.commons.exec.DefaultExecutor.setExitValues方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultExecutor.setExitValues方法的具体用法?Java DefaultExecutor.setExitValues怎么用?Java DefaultExecutor.setExitValues使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.exec.DefaultExecutor
的用法示例。
在下文中一共展示了DefaultExecutor.setExitValues方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execToFile
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
/**
* 日志文件输出方式
*
* 优点:支持将目标数据实时输出到指定日志文件中去
* 缺点:
* 标准输出和错误输出优先级固定,可能和脚本中顺序不一致
* Java无法实时获取
*
* @param command
* @param scriptFile
* @param logFile
* @param params
* @return
* @throws IOException
*/
public static int execToFile(String command, String scriptFile, String logFile, String... params) throws IOException {
// 标准输出:print (null if watchdog timeout)
// 错误输出:logging + 异常 (still exists if watchdog timeout)
// 标准输入
FileOutputStream fileOutputStream = new FileOutputStream(logFile, true);
PumpStreamHandler streamHandler = new PumpStreamHandler(fileOutputStream, fileOutputStream, null);
// command
CommandLine commandline = new CommandLine(command);
commandline.addArgument(scriptFile);
if (params!=null && params.length>0) {
commandline.addArguments(params);
}
// exec
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
exec.setStreamHandler(streamHandler);
int exitValue = exec.execute(commandline); // exit code: 0=success, 1=error
return exitValue;
}
示例2: execute
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public int execute(String[] args, @Nullable Path workingDir, Map<String, String> addEnv) throws IOException {
if (!Files.isExecutable(file)) {
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(file, perms);
}
ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
CommandLine cmd = new CommandLine(file.toFile());
cmd.addArguments(args);
DefaultExecutor exec = new DefaultExecutor();
exec.setWatchdog(watchdog);
exec.setStreamHandler(createStreamHandler());
exec.setExitValues(null);
if (workingDir != null) {
exec.setWorkingDirectory(workingDir.toFile());
}
in.close();
LOG.info("Executing: {}", cmd.toString());
Map<String, String> env = new HashMap<>(System.getenv());
env.putAll(addEnv);
return exec.execute(cmd, env);
}
示例3: execute
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
/**
* Executes the given command synchronously.
*
* @param command The command to execute.
* @param processInput Input provided to the process.
* @return The result of the execution, or empty if the process does not terminate within the timeout set for this executor.
* @throws IOException if the process execution failed.
*/
public Optional<ProcessResult> execute(String command, String processInput) throws IOException {
ByteArrayOutputStream processErr = new ByteArrayOutputStream();
ByteArrayOutputStream processOut = new ByteArrayOutputStream();
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(createStreamHandler(processOut, processErr, processInput));
ExecuteWatchdog watchDog = new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(timeoutSeconds));
executor.setWatchdog(watchDog);
executor.setExitValues(successExitCodes);
int exitCode;
try {
exitCode = executor.execute(CommandLine.parse(command));
} catch (ExecuteException e) {
exitCode = e.getExitValue();
}
return (watchDog.killedProcess()) ?
Optional.empty() : Optional.of(new ProcessResult(exitCode, processOut.toString(), processErr.toString()));
}
示例4: executeCommand
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public static int executeCommand(String command,ExecuteWatchdog watchdog) {
CommandLine cmdLine = CommandLine.parse(command);
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(new PumpStreamHandler(System.out,System.err, null));
executor.setExitValues(new int[]{0, 1});
if(watchdog != null){
executor.setWatchdog(watchdog);
}
int exitValue = 0;
try {
exitValue = executor.execute(cmdLine);
} catch (IOException e) {
exitValue = 1;
log.error("error executing command", e);
}
return exitValue;
}
示例5: runOnLocalhost
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public static ExecuteResult runOnLocalhost(ExecExceptionHandling exceptionHandling, CommandLine cmdLineOnLocalhost)
throws ExecuteException, IOException {
DefaultExecutor executor = new DefaultExecutor();
OutputsToStringStreamHandler streamHandler = new OutputsToStringStreamHandler();
executor.setStreamHandler(streamHandler);
executor.setExitValues(null);
int exitValue = executor.execute(cmdLineOnLocalhost);
ExecuteResult result = new ExecuteResult(streamHandler.getStandardOutput(), streamHandler.getStandardError(),
exitValue);
switch (exceptionHandling) {
case RETURN_EXIT_CODE_WITHOUT_THROWING_EXCEPTION:
break;
case THROW_EXCEPTION_IF_EXIT_CODE_NOT_0:
if (exitValue != 0) {
throw new ExecuteException("Failed to execute " + cmdLineOnLocalhost + " - Output: " + result,
exitValue);
}
break;
default:
break;
}
return result;
}
示例6: launchContrailServer
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public static void launchContrailServer(int port) throws Exception {
try {
DefaultExecutor exec = new DefaultExecutor();
int exitValues[] = {1};
exec.setExitValues(exitValues);
String workingDir = System.getProperty("user.dir");
String path = workingDir + "/../../config/api-server/tests/";
File f = new File(path);
exec.setWorkingDirectory(f);
exec.setStreamHandler(new PumpStreamHandler(new ByteArrayOutputStream()));
CommandLine cmd = buildServerLaunchCmd(port);
ExecuteResultHandler handler = null;
exec.execute(cmd, handler);
/* sleep 5 seconds for server to get started */
Thread.sleep(5000);
} catch (Exception e) {
s_logger.debug(e);
String cause = e.getMessage();
if (cause.equals("python: not found"))
System.out.println("No python interpreter found.");
throw e;
}
}
示例7: execute
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public CommandResult execute(CommandLine commandLine, File workingDirectory, OutputStream outputStream) {
CommandResult commandResult = new CommandResult();
try {
// create executor for command
DefaultExecutor executor = new DefaultExecutor();
// set working directory
if (workingDirectory != null) {
executor.setWorkingDirectory(workingDirectory);
}
// set possible exit values
executor.setExitValues(IntStream.range(0, 255).toArray());
// create output stream for command
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
TeeOutputStream teeOutputStream = new TeeOutputStream(outputStream, byteArrayOutputStream);
PumpStreamHandler streamHandler = new PumpStreamHandler(teeOutputStream);
executor.setStreamHandler(streamHandler);
// execute command
int exitValue = executor.execute(commandLine);
// fill command result
commandResult.setOutput(byteArrayOutputStream.toString());
commandResult.setSuccess(exitValue == 0 ? true : false);
}
catch(IOException e) {
LOG.error(e.getMessage(), e);
}
return commandResult;
}
示例8: bash
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
/**
* using bash to execute the lines.
*
* @param lines
* the command lines
* @param out
* the output stream
* @param err
* the error stream
* @param in
* the input stream
* @param workdir
* the workdir
* @return the int
* @throws IOException
* throw IOException if error
*/
public static int bash(String lines, OutputStream out, OutputStream err, InputStream in, String workdir)
throws IOException {
File f = new File(UID.id(lines).toLowerCase() + ".bash");
try {
if (!X.isEmpty(workdir)) {
lines = "cd " + workdir + ";" + lines;
}
FileUtils.writeStringToFile(f, lines);
// Execute the file we just creted. No flags are due if it is
// executed with bash directly
CommandLine commandLine = CommandLine.parse("bash " + f.getCanonicalPath());
ExecuteStreamHandler stream = new PumpStreamHandler(out, err, in);
DefaultExecutor executor = new DefaultExecutor();
int[] exit = new int[3];
for (int i = 0; i < exit.length; i++) {
exit[i] = i - 1;
}
executor.setExitValues(exit);
executor.setStreamHandler(stream);
if (!X.isEmpty(workdir)) {
executor.setWorkingDirectory(new File(workdir));
}
return executor.execute(commandLine);
} catch (IOException e) {
log.error("lines=" + lines, e);
throw e;
} finally {
f.delete();
}
}
示例9: prepareDefaultExecutor
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
DefaultExecutor executor = new ExecDefaultExecutor();
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;
}
示例10: prepareDefaultExecutor
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
@Override
protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) {
DefaultExecutor executor = new DefaultExecutorMock();
executor.setExitValues(null);
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
return executor;
}
示例11: getDefaultExecutor
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
private static DefaultExecutor getDefaultExecutor(File dir, int expectedExit, long timeout) {
DefaultExecutor executor = new DefaultExecutor();
if(expectedExit != -1) {
executor.setExitValue(expectedExit);
} else {
executor.setExitValues(null);
}
ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
executor.setWorkingDirectory(dir);
return executor;
}
示例12: executeCommand
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public static int executeCommand(final CommandLine line, final Map<String, String> environmentVariables,
final boolean verbose) {
getLogger().debug("Execute CasperJS command [" + line + "], with env: " + environmentVariables);
try {
final DefaultExecutor executor = new DefaultExecutor();
executor.setExitValues(new int[] { 0, 1 });
return executor.execute(line, environmentVariables);
} catch (final IOException e) {
if (verbose) {
getLogger().error("Could not run CasperJS command", e);
}
return -1;
}
}
示例13: test01
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
@Test
public void test01() throws ExecuteException, IOException {
try {
String command = "java -version";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
CommandLine commandline = CommandLine.parse(command);
DefaultExecutor exec = new DefaultExecutor();
exec.setExitValues(null);
PumpStreamHandler streamHandler = new PumpStreamHandler(
outputStream, errorStream);
exec.setStreamHandler(streamHandler);
exec.execute(commandline);
String out = outputStream.toString("gbk");
String error = errorStream.toString("gbk");
System.out.println("====================");
System.out.println(out + error);
System.out.println("====================");
} catch (Exception e) {
log.error("ping task failed.", e);
e.printStackTrace();
}
}