List of usage examples for java.lang Process waitFor
public abstract int waitFor() throws InterruptedException;
From source file:ee.ria.xroad.confproxy.util.ConfProxyHelper.java
/** * Helper method for running the configuration client script. * @param pb the configuration client script process builder * @throws Exception if errors occur when running the configuration client *//*from w w w . j a v a 2 s. co m*/ private static void runConfClient(final ProcessBuilder pb) throws Exception { int exitCode = -1; try { Process process = pb.start(); exitCode = process.waitFor(); } catch (IOException e) { log.error("IOException", e); exitCode = 2; throw e; } catch (Exception e) { log.error("Undetermined ConfigurationClient exitCode", e); //undetermined ConfigurationClient exitCode, fail in 'finally' throw e; } switch (exitCode) { case SUCCESS: break; case ERROR_CODE_CANNOT_DOWNLOAD_CONF: throw new Exception("configuration-client error (exit code " + exitCode + "), download failed"); case ERROR_CODE_EXPIRED_CONF: throw new Exception( "configuration-client error (exit code " + exitCode + "), configuration is outdated"); case ERROR_CODE_INVALID_SIGNATURE_VALUE: throw new Exception( "configuration-client error (exit code " + exitCode + "), configuration is incorrect"); case ERROR_CODE_INTERNAL: throw new Exception("configuration-client error (exit code " + exitCode + ")"); default: throw new Exception("Failed to download GlobalConf " + "(configuration-client exit code " + exitCode + "), " + "make sure configuration-client is" + "installed correctly"); } }
From source file:Exec.java
static Process execUnix(String[] cmdarray, String[] envp, File directory) throws IOException { // instead of calling command directly, we'll call the shell to change // directory and set environment variables. // start constructing the sh command line. StringBuffer buf = new StringBuffer(); if (directory != null) { // change to directory buf.append("cd '"); buf.append(escapeQuote(directory.toString())); buf.append("'; "); }/*from w ww . j av a 2 s . c o m*/ if (envp != null) { // set environment variables. Quote the value (but not the name). for (int i = 0; i < envp.length; ++i) { String nameval = envp[i]; int equals = nameval.indexOf('='); if (equals == -1) throw new IOException("environment variable '" + nameval + "' should have form NAME=VALUE"); buf.append(nameval.substring(0, equals + 1)); buf.append('\''); buf.append(escapeQuote(nameval.substring(equals + 1))); buf.append("\' "); } } // now that we have the directory and environment, run "which" // to test if the command name is found somewhere in the path. // If "which" fails, throw an IOException. String cmdname = escapeQuote(cmdarray[0]); Runtime rt = Runtime.getRuntime(); String[] sharray = new String[] { "sh", "-c", buf.toString() + " which \'" + cmdname + "\'" }; Process which = rt.exec(sharray); try { which.waitFor(); } catch (InterruptedException e) { throw new IOException("interrupted"); } if (which.exitValue() != 0) throw new IOException("can't execute " + cmdname + ": bad command or filename"); // finish in buf.append("exec \'"); buf.append(cmdname); buf.append("\' "); // quote each argument in the command for (int i = 1; i < cmdarray.length; ++i) { buf.append('\''); buf.append(escapeQuote(cmdarray[i])); buf.append("\' "); } System.out.println("executing " + buf); sharray[2] = buf.toString(); return rt.exec(sharray); }
From source file:Main.java
/** * create a child process with environment variables * @param command//from w w w.j a v a 2 s .co m * @param envVars * @return * @throws InterruptedException * @throws IOException */ public static String execUnixCommand(String[] command, Map<String, String> envVars) throws InterruptedException, IOException { /* ProcessBuilder processBuilder = new ProcessBuilder(command); if ( envVars != null ){ Map<String, String> env = processBuilder.environment(); env.clear(); env.putAll(envVars); } Process process = processBuilder.start(); processBuilder.redirectErrorStream(false); process.waitFor(); String outputWithError = loadStream(process.getInputStream()) + loadStream(process.getErrorStream()); return outputWithError; */ ProcessBuilder processBuilder = new ProcessBuilder(command); if (envVars != null) { Map<String, String> env = processBuilder.environment(); env.clear(); env.putAll(envVars); } Process process = processBuilder.start(); String output = loadStream(process.getInputStream()); String error = loadStream(process.getErrorStream()); process.waitFor(); return output + "\n" + error; }
From source file:brooklyn.util.io.FileUtil.java
public static void moveDir(File srcDir, File destDir) throws IOException, InterruptedException { if (!Os.isMicrosoftWindows()) { String cmd = "mv '" + srcDir.getAbsolutePath() + "' '" + destDir.getAbsolutePath() + "'"; Process proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); if (proc.exitValue() == 0) return; }// w ww. ja v a 2s. c o m FileUtils.moveDirectory(srcDir, destDir); }
From source file:brooklyn.util.io.FileUtil.java
public static void copyDir(File srcDir, File destDir) throws IOException, InterruptedException { if (!Os.isMicrosoftWindows()) { String cmd = "cp -R '" + srcDir.getAbsolutePath() + "' '" + destDir.getAbsolutePath() + "'"; Process proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); if (proc.exitValue() == 0) return; }//from w w w .ja v a2 s.c om FileUtils.copyDirectory(srcDir, destDir); }
From source file:com.manydesigns.elements.util.ElementsFileUtils.java
public static boolean chmod(File file, String perms) { logger.debug("chmod {} {}", perms, file.getAbsolutePath()); Runtime runtime = Runtime.getRuntime(); try {/*from ww w . j a va 2 s . co m*/ Process process = runtime.exec(new String[] { "chmod", perms, file.getAbsolutePath() }); int result = process.waitFor(); return result == 0; } catch (Exception e) { return false; } }
From source file:com.sap.prd.mobile.ios.mios.Forker.java
static int forkProcess(final PrintStream out, final File executionDirectory, final String... args) throws IOException { if (out == null) throw new IllegalArgumentException("Print stream for log handling was not provided."); if (args == null || args.length == 0) throw new IllegalArgumentException("No arguments has been provided."); for (final String arg : args) if (arg == null || arg.isEmpty()) throw new IllegalArgumentException( "Invalid argument '" + arg + "' provided with arguments '" + Arrays.asList(args) + "'."); final ProcessBuilder builder = new ProcessBuilder(args); if (executionDirectory != null) builder.directory(executionDirectory); builder.redirectErrorStream(true);//ww w .jav a 2s .c o m InputStream is = null; // // TODO: check if there is any support for forking processes in // maven/plexus // try { final Process process = builder.start(); is = process.getInputStream(); handleLog(is, out); return process.waitFor(); } catch (InterruptedException e) { throw new RuntimeException(e.getClass().getName() + " caught during while waiting for a forked process. This exception is not expected to be caught at that time.", e); } finally { // // Exception raised during close operation below are not reported. // That is actually bad. // We do not have any logging facility here and we cannot throw the // exception since this would swallow any // other exception raised in the try block. // May be we should revisit that ... // IOUtils.closeQuietly(is); } }
From source file:edu.brown.benchmark.voteresper.EPRuntimeUtil.java
public static String executeCommand(String command) { StringBuffer output = new StringBuffer(); Process p; try {/* www . j a v a 2s. co m*/ p = Runtime.getRuntime().exec(command); p.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; while ((line = reader.readLine()) != null) { output.append(line + "\n"); } } catch (Exception e) { e.printStackTrace(); } return output.toString(); }
From source file:Main.java
public static Process changeBuildProperty(Context c, String propName, String newValue) { Process p = null; try {/*from ww w .j a v a 2 s . c o m*/ remountSystem(c); p = runSuCommandAsync(c, "busybox sed -i \"s/" + propName + "=.*/" + propName + "=" + newValue + "/g\" /system/build.prop ; "); p.waitFor(); } catch (Exception d) { Log.e("Helper", "Failed to change build.prop. errcode:" + d.toString()); } return p; }
From source file:com.sonicle.webtop.core.app.util.OSInfo.java
private static String getCmdOutput(String command) { String output = null;/*from w w w. j a v a 2 s. c o m*/ try { Process pro = Runtime.getRuntime().exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream())); output = br.readLine(); pro.waitFor(); } catch (Throwable th) { /* Do nothing! */ } return output; }