本文整理汇总了Java中org.apache.commons.exec.DefaultExecutor.setWorkingDirectory方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultExecutor.setWorkingDirectory方法的具体用法?Java DefaultExecutor.setWorkingDirectory怎么用?Java DefaultExecutor.setWorkingDirectory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.exec.DefaultExecutor
的用法示例。
在下文中一共展示了DefaultExecutor.setWorkingDirectory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runMavenCommand
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
@When("I run maven with args: (.*)")
public void runMavenCommand(List<String> mvnArgs) throws IOException {
this.mvnArgs.addAll(mvnArgs);
System.out.println("Launching Maven with args <" + Joiner.on(" ").join(mvnArgs) + ">");
CommandLine cmdLine = new CommandLine(getCommandLine());
for (String mvnArg : mvnArgs) {
cmdLine.addArgument(mvnArg);
}
if (confdForCucumberLocation != null) {
cmdLine.addArgument("-Dcucumber.confd.binary.path=" + confdForCucumberLocation);
}
DefaultExecutor executor = new DefaultExecutor();
if (projectRootAsFile != null) {
executor.setWorkingDirectory(projectRootAsFile);
}
executor.setExitValue(expectedExitCode);
executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
@Override
protected void processLine(String line, int level) {
System.out.println(line);
executorOutput.add(line);
}
}));
exitCode = executor.execute(cmdLine, environment);
fullOutput = Joiner.on(LINE_SEPARATOR).join(executorOutput);
}
示例2: startJekyllCI
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
/**
* Starts the jekyll build process (jekyll build --incremental)
* @return true, if jekyll build was successful
*/
public boolean startJekyllCI() {
int exitValue = -1;
String line = JEKYLL_PATH;
ByteArrayOutputStream jekyllBuildOutput = new ByteArrayOutputStream();
CommandLine cmdLine = CommandLine.parse(line);
cmdLine.addArgument(JEKYLL_OPTION_BUILD);
cmdLine.addArgument(JEKYLL_OPTION_INCR);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(new File(LOCAL_REPO_PATH));
PumpStreamHandler streamHandler = new PumpStreamHandler(jekyllBuildOutput);
executor.setStreamHandler(streamHandler);
try {
LOGGER.info("Starting jekyll build");
exitValue = executor.execute(cmdLine);
LOGGER.info("Jekyll build command executed");
} catch (IOException e) {
LOGGER.error("Error while executing jekyll build. Error message: {}", e.getMessage());
e.printStackTrace();
return false;
}
printJekyllStatus(exitValue, jekyllBuildOutput.toString());
return true;
}
示例3: compile
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public void compile(String cwd, Submission submission) throws RuntimeException {
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
ExecuteWatchdog watchdog = new ExecuteWatchdog(watchdogTimeout);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(new File(cwd));
executor.setStreamHandler(new PumpStreamHandler(null, stderr, null));
executor.setWatchdog(watchdog);
CommandLine cmd = new CommandLine(javac);
cmd.addArgument("-J-Duser.language=en"); // force using English
cmd.addArgument("-classpath");
cmd.addArgument(cwd);
cmd.addArgument(fileName + ".java");
logger.info("Compiler cmd:\t" + cmd.toString());
try {
executor.execute(cmd);
logger.info("Compile OK");
} catch (IOException e) {
if (watchdog.killedProcess()) {
submission.setStatus(Submission.STATUS_CE);
submission.setError("Compile Time Exceeded");
logger.warn("Compile Time Exceeded:\t" + e.getMessage());
} else {
submission.setStatus(Submission.STATUS_CE);
submission.setError("Compile error");
logger.warn("Compile error:\t" + e.getMessage());
}
logger.warn(stderr.toString());
throw new RuntimeException("Compile Aborted.");
}
}
示例4: 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);
}
示例5: exec
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
void exec(String action, File dir, CommandLine cmdLine) {
String label = action + ": " + cmdLine;
try {
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(dir);
Map<String, String> environment = EnvironmentUtils.getProcEnvironment();
environment.put("PATH", environment.get("PATH") + ":" + new File(frontendDirectory, "node").getAbsolutePath());
int exitValue = executor.execute(cmdLine, environment);
if (exitValue == 0) {
getLog().info(label + ": OK");
} else {
throw new MojoExecutionException("EXEC FAILURE: " + label);
}
} catch (RuntimeException re) {
throw re;
} catch (Exception e) {
throw new IllegalStateException("EXEC FAILURE: " + label, e);
}
}
示例6: shouldNotOverwriteInputFile
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
@Test
public void shouldNotOverwriteInputFile() throws IOException, VerificationException {
SetupContent setupContent = new SetupContent().invoke();
String shadedJar = setupContent.getShadedJar();
String issue24POM = setupContent.getIssue24POM();
String issue24TestFile = setupContent.getIssue24TestFile();
File specFile = new File(issue24TestFile);
File specBackupFile = new File(issue24TestFile + ".orig");
FileUtils.copyFile(specFile, specBackupFile);
String line = String.format("java -jar %s -v -p %s %s", shadedJar, issue24POM, issue24TestFile);
CommandLine command = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(specFile.getParentFile());
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
executor.setStreamHandler(new PumpStreamHandler(out, err));
executor.execute(command);
assertTrue("The specification should not be overriden by the output", FileUtils.contentEquals(specFile,specBackupFile));
}
示例7: 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;
}
}
示例8: execASync
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
public static ExecuteWatchdog execASync(String line, File workDir, Map<String, String> environment, OutputStream output, ExecuteResultHandler erh,
long timeout) throws IOException {
if (environment != null) {
Map<String, String> sysenv = System.getenv();
for (String key : sysenv.keySet()) {
boolean contains = false;
for (String k : environment.keySet()) {
if (k.equalsIgnoreCase(key)) {
contains = true;
break;
}
}
if (!contains)
environment.put(key, sysenv.get(key));
}
}
DefaultExecutor executor = new DefaultExecutor();
if (workDir != null)
executor.setWorkingDirectory(workDir);
PumpStreamHandler sh = new PumpStreamHandler(output, output, null);
executor.setStreamHandler(sh);
ExecuteWatchdog watchdog = new ProcessTreeWatchDog(timeout);
executor.setWatchdog(watchdog);
executor.execute(CommandLine.parse(line), environment, erh);
return watchdog;
}
示例9: 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;
}
示例10: executeProcess
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
/**
* Creates a process and logs the output
*/
public void executeProcess(String[] cmd, String logKey, ProcessResult result,
Map<String, String> additionalEnvVars, File workingDir) {
Map<String, String> env = getEnvVars(cmd, additionalEnvVars);
logger.info(format("%s Cmd: %s, Additional Env Vars: %s", logKey,
String.join(" ", cmd), additionalEnvVars));
try {
CommandLine cmdLine = new CommandLine(cmd[0]);
// add rest of cmd string[] as arguments
for (int i = 1; i < cmd.length; i++) {
// needs the quote handling=false or else doesn't work
// http://www.techques.com/question/1-5080109/How-to-execute--bin-sh-with-commons-exec?
cmdLine.addArgument(cmd[i], false);
}
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(0);
executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000));
executor.setStreamHandler(new OutputHandler(logger, logKey, result));
if (workingDir != null) {
executor.setWorkingDirectory(workingDir);
}
result.setResultCode(executor.execute(cmdLine, env));
// set fault to last error if fault map is empty
if (result.getResultCode() != 0 && result.getFaultMap().keySet().size() < 1) {
result.getFaultMap().put("ERROR", result.getLastError());
}
} catch (ExecuteException ee) {
logger.error(logKey + ee);
result.setResultCode(ee.getExitValue());
} catch (IOException e) {
logger.error(e);
result.setResultCode(1);
}
}
示例11: 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();
}
}
示例12: 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;
}
示例13: executeCommand
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
private Future<CommandResult> executeCommand(String command, File executionDirectory, boolean silent)
throws IOException {
StringWriter writer = new StringWriter();
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
try (WriterOutputStream outputStream = new WriterOutputStream(writer)) {
String outerCommand = "/bin/bash -lc";
CommandLine outer = CommandLine.parse(outerCommand);
outer.addArgument(command, false);
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(executionDirectory);
executor.setStreamHandler(new PumpStreamHandler(silent ? outputStream : System.out, null));
executor.execute(outer, ENVIRONMENT, resultHandler);
resultHandler.waitFor();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return new AsyncResult<CommandResult>(
new CommandResult(resultHandler.getExitValue(), writer.toString(), resultHandler.getException()));
}
示例14: 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;
}
示例15: factorize
import org.apache.commons.exec.DefaultExecutor; //导入方法依赖的package包/类
private CommandExecutor factorize(File dir, String name, Command command) {
DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(dir);
CommandExecutorStreamHandler executorStreamHandler = new CommandExecutorStreamHandler(name);
executor.setStreamHandler(new PumpStreamHandler(executorStreamHandler));
return new CommandExecutor(command, executor, executorStreamHandler::getOutput);
}