List of usage examples for java.lang ProcessBuilder ProcessBuilder
public ProcessBuilder(String... command)
From source file:Main.java
public static Process runCommand(String... arguments) { List<String> commandLine = Lists.asList(getAdbPath(getSdkPath()), arguments); ProcessBuilder processBuilder = new ProcessBuilder(commandLine); try {//from www . j av a 2 s.c o m return processBuilder.start(); } catch (IOException e) { throw new RuntimeException("An IOException occurred when starting ADB.", e); } }
From source file:myproject.Model.Common.ProcessExecutor.java
private static String properExecute(File file, String... commands) throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder(commands); if (file != null) { builder.directory(file);//from w ww.ja va 2s. c o m } Process process = builder.start(); String input = IOUtils.toString(process.getInputStream(), "utf-8"); String error = IOUtils.toString(process.getErrorStream(), "utf-8"); //String command = Arrays.toString(builder.command().toArray(new String[] {})); process.waitFor(); process.getInputStream().close(); if (!error.isEmpty()) { Log.errorLog(error, ProcessExecutor.class); } String result = input; if (input.isEmpty()) { result = error; } return result; }
From source file:de.bamamoto.mactools.png2icns.Scaler.java
protected static String runProcess(String[] commandLine) throws IOException { StringBuilder cl = new StringBuilder(); for (String i : commandLine) { cl.append(i);//from ww w . j ava 2 s . co m cl.append(" "); } String result = ""; ProcessBuilder builder = new ProcessBuilder(commandLine); Map<String, String> env = builder.environment(); env.put("PATH", "/usr/sbin:/usr/bin:/sbin:/bin"); builder.redirectErrorStream(true); Process process = builder.start(); String line; InputStream stdout = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); while ((line = reader.readLine()) != null) { result += line + "\n"; } boolean isProcessRunning = true; int maxRetries = 60; do { try { isProcessRunning = process.exitValue() < 0; } catch (IllegalThreadStateException ex) { System.out.println("Process not terminated. Waiting ..."); try { Thread.sleep(1000); } catch (InterruptedException iex) { //nothing todo } maxRetries--; } } while (isProcessRunning && maxRetries > 0); System.out.println("Process has terminated"); if (process.exitValue() != 0) { throw new IllegalStateException("Exit value not equal to 0: " + result); } if (maxRetries == 0 && isProcessRunning) { System.out.println("Process does not terminate. Try to kill the process now."); process.destroy(); } return result; }
From source file:de.micromata.mgc.application.webserver.config.KeyTool.java
public static void generateKey(ValContext ctx, File keyFile, String storePass, String keyAlias) { String[] args = { "keytool", "-genkey", "-alias", keyAlias, "-keyalg", "RSA", "-keystore", keyFile.getAbsolutePath(), "-keysize", "2048", "-keypass", storePass, "-storepass", storePass, "-dname", "cn=Launcher, ou=MGC, o=Microamta, c=DE" }; StringBuilder oksb = new StringBuilder(); oksb.append("Execute: " + StringUtils.join(args, " ")); try {/* w w w .j a v a 2s . c om*/ ProcessBuilder pb = new ProcessBuilder(args); pb.redirectErrorStream(true); Process process = pb.start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { oksb.append(line); } boolean success = process.waitFor(5, TimeUnit.SECONDS); if (success == false) { ctx.directError(null, "Fail to wait for keytool"); } else { int exitValue = process.exitValue(); if (exitValue == 0) { oksb.append("\nSuccess"); ctx.directInfo(null, oksb.toString()); } else { ctx.directError(null, oksb.toString()); ctx.directError(null, "Failure executing keytool. ReturnCode: " + exitValue); } } } catch (Exception ex) { ctx.directError(null, "Failure executing keytool: " + ex.getMessage(), ex); } }
From source file:com.yahoo.rdl.maven.ProcessRunner.java
public String run(List<String> command) throws IOException { return run(command, new ProcessBuilder(command)); }
From source file:Main.java
private static int readSystemFileAsInt(final String pSystemFile) throws Exception { InputStream in = null;/*from w w w.j a va2 s.co m*/ try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final String content = readFully(in); return Integer.parseInt(content); } catch (final Exception e) { throw new Exception(e); } }
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);//from w w w . ja v a 2 s .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:Main.java
public static MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon) throws Exception { InputStream in = null;//from ww w . j a v a2 s. c o m try { final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start(); in = process.getInputStream(); final Scanner scanner = new Scanner(in); final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null; if (matchFound) { return scanner.match(); } else { throw new Exception(); } } catch (final IOException e) { throw new Exception(e); } }
From source file:Main.java
/** * create a child process with environment variables * @param command// w ww . ja v a 2s. 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:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void compress(Path sourceDirectoryPath, Path archiveFilePath) throws Exception { try {// ww w .j a v a 2s .c o m ImmutableList<String> tarArgs = Lists.immutable.of(UNIX_TAR, "cvzf", archiveFilePath.toFile().getAbsolutePath(), "."); Process process = new ProcessBuilder(tarArgs.toList()).directory(sourceDirectoryPath.toFile()).start(); int exitCode = process.waitFor(); if (exitCode != 0) { logStdout(process.getInputStream()); logStdErr(process.getErrorStream()); throw new Exception("Failed to compress"); } } catch (Exception e) { throw new Exception("Failed to compress", e); } }