List of usage examples for java.lang Process getOutputStream
public abstract OutputStream getOutputStream();
From source file:Main.java
public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null; int exitCode = -1; if (runAsRoot) proc = Runtime.getRuntime().exec("su"); else/* w ww . j ava2 s .c o m*/ proc = Runtime.getRuntime().exec("sh"); OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); // TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); out.write(cmd); out.write("\n"); out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } exitCode = proc.waitFor(); } return exitCode; }
From source file:Main.java
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null; int exitCode = -1; if (runAsRoot) proc = Runtime.getRuntime().exec("su"); else//from w w w .j a v a 2 s. c om proc = Runtime.getRuntime().exec("sh"); OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); for (int i = 0; i < cmds.length; i++) { Log.d("the-onion-phone", "executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); out.write(cmds[i]); out.write("\n"); } out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } exitCode = proc.waitFor(); } return exitCode; }
From source file:Main.java
static void execPrivileged(final String[] cmd_array) throws Exception { try {//from w w w . ja v a 2s. c o m Process process = AccessController.doPrivileged(new PrivilegedExceptionAction<Process>() { public Process run() throws Exception { return Runtime.getRuntime().exec(cmd_array); } }); // Close unused streams to make sure the child process won't hang process.getInputStream().close(); process.getOutputStream().close(); process.getErrorStream().close(); } catch (PrivilegedActionException e) { throw (Exception) e.getCause(); } }
From source file:ReadTemp.java
/** Executes the given applescript code and returns the first line of the output as a string *///from w w w. j av a 2s .c o m static String doApplescript(String script) { String line; try { // Start applescript Process p = Runtime.getRuntime().exec("/usr/bin/osascript -s o -"); // Send applescript via stdin OutputStream stdin = p.getOutputStream(); stdin.write(script.getBytes()); stdin.flush(); stdin.close(); // get first line of output BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); line = input.readLine(); input.close(); // If we get an exit code, print it out if (p.waitFor() != 0) { System.err.println("exit value = " + p.exitValue()); System.err.println(line); } return line; } catch (Exception e) { System.err.println(e); } return ""; }
From source file:com.chintans.venturebox.util.Utils.java
public static String exec(String command) { try {/* w w w .j a v a2 s . c o m*/ Process p = Runtime.getRuntime().exec(command); DataOutputStream os = new DataOutputStream(p.getOutputStream()); os.writeBytes("sync\n"); os.writeBytes("exit\n"); os.flush(); p.waitFor(); return getStreamLines(p.getInputStream()); } catch (Exception ex) { ex.printStackTrace(); return null; } }
From source file:com.sinpo.xnfc.nfc.Util.java
public static String execRootCmd(String cmd) { String result = ""; DataOutputStream dos = null;/*from ww w . j a v a 2 s . co m*/ DataInputStream dis = null; try { Process p = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(p.getOutputStream()); dis = new DataInputStream(p.getInputStream()); dos.writeBytes(cmd + "\n"); dos.flush(); dos.writeBytes("exit\n"); dos.flush(); String line = null; while ((line = dis.readLine()) != null) { result += line + "\r\n"; } p.waitFor(); } catch (Exception e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } if (dis != null) { try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
From source file:Main.java
private static List<Integer> getAllRelatedPids(final int pid) { List<Integer> result = new ArrayList<Integer>(Arrays.asList(pid)); // use 'ps' to get this pid and all pids that are related to it (e.g. // spawned by it) try {/*from w w w .j av a2 s . c om*/ final Process suProcess = Runtime.getRuntime().exec("su"); new Thread(new Runnable() { @Override public void run() { PrintStream outputStream = null; try { outputStream = new PrintStream(new BufferedOutputStream(suProcess.getOutputStream(), 8192)); outputStream.println("ps"); outputStream.println("exit"); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } } }).run(); if (suProcess != null) { try { suProcess.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } } BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new InputStreamReader(suProcess.getInputStream()), 8192); while (bufferedReader.ready()) { String[] line = SPACES_PATTERN.split(bufferedReader.readLine()); if (line.length >= 3) { try { if (pid == Integer.parseInt(line[2])) { result.add(Integer.parseInt(line[1])); } } catch (NumberFormatException ignore) { } } } } finally { if (bufferedReader != null) { bufferedReader.close(); } } } catch (IOException e1) { e1.printStackTrace(); } return result; }
From source file:com.alibaba.otter.shared.common.utils.cmd.Exec.java
public static Result execute(Process process, String cmd, String outputLog, byte[] input, File workingDir) throws Exception { // ???//from w w w .j ava 2 s . com Thread inputThread = new InputPumper((input == null) ? new byte[] {} : input, process.getOutputStream()); StreamCollector stderr = null; StreamCollector stdout = null; FileOutputStream fileOutput = null; StreamAppender outputLogger = null; String errString = null; String outString = null; try { if (outputLog == null) { stdout = new StreamCollector(process.getInputStream()); stderr = new StreamCollector(process.getErrorStream()); stdout.start(); stderr.start(); } else { errString = "stderr output redirected to file " + outputLog; outString = "stdout output redirected to file " + outputLog; fileOutput = new FileOutputStream(outputLog); outputLogger = new StreamAppender(fileOutput); outputLogger.writeInput(process.getErrorStream(), process.getInputStream()); } inputThread.start(); final int exitCode = process.waitFor(); inputThread.join(); if (outputLogger != null) { outputLogger.finish(); } if (stdout != null) { stdout.join(); outString = stdout.toString(); } if (stderr != null) { stderr.join(); errString = stderr.toString(); } return new Result(cmd.toString(), outString, errString, exitCode); } finally { IOUtils.closeQuietly(fileOutput); if (process != null) { // evitons http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6462165 process.getInputStream().close(); process.getOutputStream().close(); process.getErrorStream().close(); process.destroy(); } } }
From source file:Main.java
public static String[] execSQL(String dbName, String query) { Process process = null; Runtime runtime = Runtime.getRuntime(); OutputStreamWriter outputStreamWriter; try {//from ww w . j av a2 s . com String command = dbName + " " + "'" + query + "'" + ";"; process = runtime.exec("su"); outputStreamWriter = new OutputStreamWriter(process.getOutputStream()); outputStreamWriter.write("sqlite3 " + command); outputStreamWriter.flush(); outputStreamWriter.close(); outputStreamWriter.close(); } catch (IOException e) { e.printStackTrace(); } final InputStreamReader errorStreamReader = new InputStreamReader(process.getErrorStream()); (new Thread() { @Override public void run() { try { BufferedReader bufferedReader = new BufferedReader(errorStreamReader); String s; while ((s = bufferedReader.readLine()) != null) { Log.d("com.suraj.waext", "WhatsAppDBHelper:" + s); } } catch (Exception ex) { ex.printStackTrace(); } } }).start(); try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String s; StringBuilder op = new StringBuilder(); while ((s = bufferedReader.readLine()) != null) { op.append(s).append("\n"); } return op.toString().split("\n"); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:org.nibblesec.tools.BurpUtils.java
public static void executeAndPipe(String cmdLine, byte[] stdInputData, IBurpExtenderCallbacks callbacks, boolean waitFor) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(cmdLine.split(" ")); callbacks.issueAlert("Executing: " + cmdLine); final Process process; process = pb.start();/*w ww . j a va 2s . c o m*/ pipe(process.getErrorStream(), new PrintStream(callbacks.getStderr())); pipe(process.getInputStream(), new PrintStream(callbacks.getStdout())); if (stdInputData != null) { //Stream more data to stdIn OutputStream stdin = process.getOutputStream(); IOUtils.write(stdInputData, stdin); IOUtils.closeQuietly(stdin); } if (waitFor) { process.waitFor(); } }