List of usage examples for java.lang Process waitFor
public abstract int waitFor() throws InterruptedException;
From source file:Main.java
/** * Run a command and return its output./*from w w w .j ava 2 s . c om*/ */ public static String systemOut(String command) throws Exception { int c; String cmd[] = new String[3]; cmd[0] = System.getProperty("SHELL", "/bin/sh"); cmd[1] = "-c"; cmd[2] = command; Process p = Runtime.getRuntime().exec(cmd); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder s = new StringBuilder(); while ((c = r.read()) != -1) s.append((char) c); p.waitFor(); return s.toString(); }
From source file:abs.backend.erlang.ErlangBackend.java
public static void compile(Model m, File destDir, boolean verbose) throws IOException, InterruptedException, InternalBackendException { if (verbose)//from www . ja v a 2 s.c o m System.out.println("Generating Erlang code..."); // check erlang version number Process versionCheck = Runtime.getRuntime().exec(new String[] { "erl", "-eval", "io:fwrite(\"~s\n\", [erlang:system_info(otp_release)]), halt().", "-noshell" }); versionCheck.waitFor(); BufferedReader ir = new BufferedReader(new InputStreamReader(versionCheck.getInputStream())); int version = Integer.parseInt(ir.readLine()); if (version < minVersion) { String message = "ABS requires at least erlang version " + Integer.toString(minVersion) + ", installed version is " + Integer.toString(version); throw new InternalBackendException(message); } ErlApp erlApp = new ErlApp(destDir); m.generateErlangCode(erlApp); erlApp.close(); String[] rebarProgram = new String[] { "escript", "../bin/rebar", "compile" }; Process p = Runtime.getRuntime().exec(rebarProgram, null, new File(destDir, "absmodel")); if (verbose) IOUtils.copy(p.getInputStream(), System.out); p.waitFor(); if (p.exitValue() != 0) { String message = "Compilation of generated erlang code failed with exit value " + p.exitValue(); if (!verbose) message = message + "\n (use -v for detailed compiler output)"; throw new InternalBackendException(message); // TODO: consider removing the generated code here. For now, // let's leave it in place for diagnosis. } else { if (verbose) { System.out.println(); System.out.println("Finished. \"gen/erl/run\" to start the model."); System.out.println(" (\"gen/erl/run --help\" for more options)"); } } }
From source file:Main.java
public static int exec(String command) { Process proc = null; BufferedReader error = null;//from ww w . j av a 2s .com try { proc = Runtime.getRuntime().exec(command); error = toReader(proc.getErrorStream()); int retVal = proc.waitFor(); if (retVal != 0) { String line; while ((line = error.readLine()) != null) { Log.d(TAG, "exec error:" + line); } } return retVal; } catch (Exception e) { Log.d(TAG, e.getMessage()); if (proc != null) { proc.destroy(); } } return -1; }
From source file:Main.java
public static int execRootCmdSilent(String cmd) { int result = -1; DataOutputStream dos = null;//from w w w . j a v a 2 s .c o m try { Process p = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(p.getOutputStream()); dos.writeBytes(cmd + "\n"); dos.flush(); dos.writeBytes("exit\n"); dos.flush(); p.waitFor(); result = p.exitValue(); } catch (Exception e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
From source file:com.surfs.storage.common.util.CmdUtils.java
public static CmdResponse executeCommand(String cmd) { BufferedReader bufRead = null; try {//from w w w .j a v a2s . c om //bufRead = executeCmdForReader(cmd); Process pro = Runtime.getRuntime().exec(cmd); bufRead = new BufferedReader(new InputStreamReader(pro.getInputStream(), "UTF-8")); // 0-success,others-failure int status = pro.waitFor(); String response = bufRead.readLine(); LogFactory.info("cmd:" + cmd); LogFactory.info("status:" + status); LogFactory.info("response:" + response); return new CmdResponse(status, response); } catch (Exception e) { return new CmdResponse(500, e.getMessage()); } finally { if (bufRead != null) try { bufRead.close(); } catch (Exception e) { return new CmdResponse(500, e.getMessage()); } } }
From source file:io.github.retz.executor.FileManager.java
private static void fetchHDFSFile(String file, String dest) throws IOException { LOG.debug("Downloading {} to {} as HDFS file", file, dest); // TODO: make 'hadoop' command arbitrarily specifiable, but given that mesos-agent (slave) can fetch hdfs:// files, it should be also available, too String[] hadoopCmd = { "hadoop", "fs", "-copyToLocal", file, dest }; LOG.debug("Command: {}", String.join(" ", hadoopCmd)); ProcessBuilder pb = new ProcessBuilder(); pb.command(hadoopCmd).inheritIO();/*ww w. j a v a 2s. co m*/ Process p = pb.start(); while (true) { try { int result = p.waitFor(); if (result != 0) { LOG.error("Downloading {} failed: {}", file, result); } else { LOG.info("Download finished: {}", file); } return; } catch (InterruptedException e) { LOG.error("Download process interrupted: {}", e.getMessage()); // TODO: debug? } } }
From source file:ml.shifu.shifu.executor.ProcessManager.java
public static int runShellProcess(String currentDir, String[] args) throws IOException { ProcessBuilder processBuilder = new ProcessBuilder(args); processBuilder.directory(new File(currentDir)); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); LogThread logThread = new LogThread(process, process.getInputStream(), currentDir); logThread.start();// www . j ava 2s.c o m try { process.waitFor(); } catch (InterruptedException e) { process.destroy(); } finally { logThread.setToQuit(true); } LOG.info("Under {} directory, finish run `{}`", currentDir, args); return process.exitValue(); }
From source file:br.pucminas.ri.jsearch.rest.controller.ApiController.java
private static Object evalTests(Request req, Response res) { try {//from w w w . java2 s . c o m Searcher.performTests("result/queries.txt"); String[] command = { "./tests.sh" }; Process p = Runtime.getRuntime().exec(command); p.waitFor(); Path path = Paths.get("result/output.txt"); res.header("Content-Disposition", "attachment; filename=output.txt"); res.type("application/force-download"); byte[] data = null; try { data = Files.readAllBytes(path); } catch (Exception e1) { e1.printStackTrace(); } res.raw().getOutputStream().write(data); try { res.raw().getOutputStream().write(data); res.raw().getOutputStream().flush(); res.raw().getOutputStream().close(); } catch (Exception e) { e.printStackTrace(); } return res.raw(); } catch (IOException | InterruptedException e) { e.printStackTrace(); return "Error on execute tests."; } }
From source file:Main.java
/** * Run a command and return its output./* ww w.java 2s .c om*/ */ public static String systemOut(String command) throws Exception { int c; String[] cmd = new String[3]; cmd[0] = System.getProperty("SHELL", "/bin/sh"); cmd[1] = "-c"; cmd[2] = command; Process p = Runtime.getRuntime().exec(cmd); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder s = new StringBuilder(); while ((c = r.read()) != -1) { s.append((char) c); } p.waitFor(); return s.toString(); }
From source file:Main.java
public static void runAsRoot(String[] commands) throws IOException, InterruptedException { Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); for (String cmd : commands) { os.writeBytes(cmd + "\n"); }/*from ww w. ja v a 2 s . co m*/ os.writeBytes("exit\n"); os.flush(); os.close(); p.waitFor(); }