本文整理匯總了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();
}
}