List of usage examples for org.apache.commons.exec DefaultExecutor setWorkingDirectory
public void setWorkingDirectory(final File dir)
From source file:generator.Utils.java
public static boolean exec(String command, File workingDir, String... args) { try {/*from ww w .j a v a 2s . c o m*/ CommandLine cmdLine = new CommandLine(command); cmdLine.addArguments(args, false); DefaultExecutor exe = new DefaultExecutor(); exe.setWorkingDirectory(workingDir); return exe.execute(cmdLine) == 0; } catch (Exception e) { return false; } }
From source file:com.jaeksoft.searchlib.util.ExecuteUtils.java
final public static int command(File workingDirectory, String cmd, String classpath, boolean setJavaTempDir, OutputStream outputStream, OutputStream errorStream, Long timeOut, String... arguments) throws ExecuteException, IOException { Map<String, String> envMap = null; if (classpath != null) { envMap = new HashMap<String, String>(); envMap.put("CLASSPATH", classpath); }/*from w w w . j a v a2 s . c o m*/ CommandLine commandLine = new CommandLine(cmd); if (setJavaTempDir) if (!StringUtils.isEmpty(SystemUtils.JAVA_IO_TMPDIR)) commandLine.addArgument(StringUtils.fastConcat("-Djava.io.tmpdir=", SystemUtils.JAVA_IO_TMPDIR), false); if (arguments != null) for (String argument : arguments) commandLine.addArgument(argument); DefaultExecutor executor = new DefaultExecutor(); if (workingDirectory != null) executor.setWorkingDirectory(workingDirectory); if (outputStream != null) { PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream); executor.setStreamHandler(pumpStreamHandler); } if (timeOut != null) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeOut); executor.setWatchdog(watchdog); } return envMap != null ? executor.execute(commandLine, envMap) : executor.execute(commandLine); }
From source file:com.mooregreatsoftware.gitprocess.lib.Pusher.java
private static ThePushResult doGitProgPush(GitLib gitLib, Branch localBranch, String remoteBranchName, boolean forcePush, String remoteName) { String cmd = String.format("git push --porcelain %s %s %s:%s", remoteName, forcePush ? "--force" : "", localBranch.shortName(), remoteBranchName); CommandLine commandLine = CommandLine.parse(cmd); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(gitLib.workingDirectory()); final StringWriter stdOutWriter = new StringWriter(); final StringWriter stdErrWriter = new StringWriter(); executor.setStreamHandler(/*from w w w . j a v a 2 s . c o m*/ new PumpStreamHandler(new WriterOutputStream(stdOutWriter), new WriterOutputStream(stdErrWriter))); final int exitCode = Try.of(() -> { try { return executor.execute(commandLine); } catch (ExecuteException e) { return e.getExitValue(); } catch (IOException e) { final String message = e.getMessage(); if (message != null && message.contains("No such file or directory")) { return 1; } e.printStackTrace(); return -1; } }).get(); return new ProcPushResult(stdOutWriter, stdErrWriter, exitCode); }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
@DescribeProcess(title = "r.viewshed", description = "Computes the viewshed of a point on an elevation raster map.") @DescribeResult(description = "area visible from provided location") public static GridCoverage2D viewshed( @DescribeParameter(name = "dem", description = "digitial elevation model") GridCoverage2D dem, @DescribeParameter(name = "x", description = "x location in map units") double x, @DescribeParameter(name = "y", description = "y location in map units") double y) throws Exception { String COMMAND = "viewshed"; //Stage files in a temporary location File geodb = Files.createTempDirectory("grassdata").toFile(); geodb.deleteOnExit();/* ww w .jav a 2 s . c o m*/ File location = new File(geodb, COMMAND + Long.toString(random.nextLong()) + "location"); // stage dem file File file = new File(geodb, "dem.tif"); //The file must exist for FileImageOutputStreamExtImplSpi to create the output stream if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } final GeoTiffFormat format = new GeoTiffFormat(); GridCoverageWriter writer = format.getWriter(file); writer.write(dem, null); LOGGER.info("Staging file:" + file); // use file to create location with (returns PERMANENT mapset) File mapset = location(location, file); DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(60000)); executor.setStreamHandler(new PumpStreamHandler(System.out)); executor.setWorkingDirectory(mapset); Map<String, String> env = customEnv(geodb, location, mapset); // EXPORT IMPORT DEM // r.in.gdal input=~/grassdata/viewshed/PERMANENT/dem.tif output=dem --overwrite File r_in_gdal = bin("r.in.gdal"); CommandLine cmd = new CommandLine(r_in_gdal); cmd.addArgument("input=${file}"); cmd.addArgument("output=dem"); cmd.addArgument("--overwrite"); cmd.setSubstitutionMap(new KVP("file", file)); try { LOGGER.info(cmd.toString()); executor.setExitValue(0); int exitValue = executor.execute(cmd, env); } catch (ExecuteException fail) { LOGGER.warning(r_in_gdal.getName() + ":" + fail.getLocalizedMessage()); throw fail; } // EXECUTE VIEWSHED File r_viewshed = bin("r.viewshed"); cmd = new CommandLine(r_viewshed); cmd.addArgument("input=dem"); cmd.addArgument("output=viewshed"); cmd.addArgument("coordinates=${x},${y}"); cmd.addArgument("--overwrite"); cmd.setSubstitutionMap(new KVP("x", x, "y", y)); try { LOGGER.info(cmd.toString()); executor.setExitValue(0); int exitValue = executor.execute(cmd, env); } catch (ExecuteException fail) { LOGGER.warning(r_viewshed.getName() + ":" + fail.getLocalizedMessage()); throw fail; } // EXECUTE EXPORT VIEWSHED // r.out.gdal --overwrite input=viewshed@PERMANENT output=/Users/jody/grassdata/viewshed/viewshed.tif format=GTiff File viewshed = new File(location, "viewshed.tif"); File r_out_gdal = bin("r.out.gdal"); cmd = new CommandLine(r_out_gdal); cmd.addArgument("input=viewshed"); cmd.addArgument("output=${viewshed}"); cmd.addArgument("--overwrite"); cmd.addArgument("format=GTiff"); cmd.setSubstitutionMap(new KVP("viewshed", viewshed)); try { LOGGER.info(cmd.toString()); executor.setExitValue(0); int exitValue = executor.execute(cmd, env); } catch (ExecuteException fail) { LOGGER.warning(r_out_gdal.getName() + ":" + fail.getLocalizedMessage()); throw fail; } // STAGE RESULT if (!viewshed.exists()) { throw new IOException("Generated viweshed.tif not found"); } GeoTiffReader reader = format.getReader(viewshed); GridCoverage2D coverage = reader.read(null); cleanup(new File(env.get("GISRC"))); return coverage; }
From source file:com.rest4j.generator.PHPGeneratorTest.java
@Test public void testPHPClient() throws Exception { gen.setStylesheet("com/rest4j/client/php.xslt"); new File("target/php").mkdir(); gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml")); gen.setOutputDir("target/php"); gen.addParam(new TemplateParam("common-params", "access-token")); gen.addParam(new TemplateParam("prefix", "Test")); gen.addParam(new TemplateParam("additional-client-code", "// ADDITIONAL CODE")); gen.generate();/*from www . j av a 2 s .c o m*/ // check the syntax CommandLine cmdLine = CommandLine.parse("php apiclient.php"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File("target/php")); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); assertFalse(executor.isFailure(exitValue)); // check existence of Parameter Object class String client = IOUtils.toString(new File("target/php/apiclient.php").toURI()); assertTrue(client, client.contains("class TestPatchBRequest")); assertTrue(client.contains("ADDITIONAL CODE")); }
From source file:com.rest4j.generator.PythonGeneratorTest.java
@Test public void testPythonClient() throws Exception { gen.setStylesheet("com/rest4j/client/python.xslt"); new File("target/python").mkdir(); gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml")); gen.setOutputDir("target/python"); gen.addParam(new TemplateParam("common-params", "access-token")); gen.addParam(new TemplateParam("additional-client-code", "\t# ADDITIONAL CODE")); gen.generate();//from w w w.j a va 2s . c o m // let's try compiling the damn thing CommandLine cmdLine = CommandLine.parse("python apiclient.py"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File("target/python")); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); assertFalse(executor.isFailure(exitValue)); String a = IOUtils.toString(new File("target/python/apiclient.py").toURI()); // check doc comments assertTrue(a, a.contains("Some additional client info")); assertTrue(a, a.contains("Some additional python client info")); // check existence of Parameter Object class assertTrue(a, a.contains("class PatchBRequest")); // check additional-client-code assertTrue(a, a.contains("\t# ADDITIONAL CODE")); }
From source file:com.rest4j.generator.JavaGeneratorTest.java
@Test public void testJavaClient() throws Exception { gen.setStylesheet("com/rest4j/client/java.xslt"); new File("target/java").mkdir(); gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml")); gen.setOutputDir("target/java"); gen.addParam(new TemplateParam("common-params", "access-token")); gen.addParam(new TemplateParam("additional-client-code", "// ADDITIONAL CODE")); gen.generate();/* w ww. j a v a2 s . c o m*/ // let's try compiling the damn thing CommandLine cmdLine = CommandLine.parse("mvn package"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File("target/java")); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); assertFalse(executor.isFailure(exitValue)); // check doc comments in the file A.java String a = IOUtils.toString(new File("target/java/src/main/java/api/model/A.java").toURI()); assertTrue(a, a.contains("Some additional client info")); assertFalse(a, a.contains("Some additional python client info")); // check existence of Parameter Object class a = IOUtils.toString(new File("target/java/src/main/java/api/model/PatchBRequest.java").toURI()); assertTrue(a, a.contains("class PatchBRequest")); // check some file paths assertTrue(new File("target/java/src/main/java/api/util/JsonUtil.java").canRead()); assertTrue(new File("target/java/src/main/java/api/Request.java").canRead()); String client = IOUtils.toString(new File("target/java/src/main/java/api/Client.java").toURI()); assertTrue(client.contains("ADDITIONAL CODE")); }
From source file:com.github.trecloux.yeoman.YeomanMojo.java
void executeCommand(String command) throws MojoExecutionException { try {/* w w w. ja v a 2 s .com*/ if (isWindows()) { command = "cmd /c " + command; } CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(yeomanProjectDirectory); executor.execute(cmdLine); } catch (IOException e) { throw new MojoExecutionException("Error during : " + command, e); } }
From source file:com.alibaba.jstorm.yarn.utils.JStormUtils.java
/** * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result * <p/>//from w w w . jav a 2 s . co m * This function don't care whether the command is successfully or not * * @param command * @param environment * @param workDir * @param resultHandler * @return * @throws IOException */ @Deprecated public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler) throws IOException { String[] cmdlist = command.split(" "); CommandLine cmd = new CommandLine(cmdlist[0]); for (String cmdItem : cmdlist) { if (StringUtils.isBlank(cmdItem) == false) { cmd.addArgument(cmdItem); } } DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); if (StringUtils.isBlank(workDir) == false) { executor.setWorkingDirectory(new File(workDir)); } ByteArrayOutputStream out = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out, out); if (streamHandler != null) { executor.setStreamHandler(streamHandler); } try { if (resultHandler == null) { executor.execute(cmd, environment); } else { executor.execute(cmd, environment, resultHandler); } } catch (ExecuteException e) { // @@@@ // failed to run command } return out; }
From source file:fitnesse.maven.io.CommandShell.java
public String execute(File workingDir, String... commands) { CommandLine commandLine = CommandLine.parse(commands[0]); for (int i = 1; i < commands.length; i++) { commandLine.addArgument(commands[i]); }/*from ww w . j a v a2s .c o m*/ DefaultExecutor executor = new DefaultExecutor(); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(baos)); executor.setWorkingDirectory(workingDir); executor.execute(commandLine); return new String(baos.toByteArray()); } catch (ExecuteException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }