List of usage examples for java.lang Process getOutputStream
public abstract OutputStream getOutputStream();
From source file:com.qq.tars.tools.SystemUtils.java
public static Pair<Integer, Pair<String, String>> exec(String command) { log.info("start to exec shell, command={}", command); try {//w ww . j ava 2 s . c o m Process process = Runtime.getRuntime().exec("/bin/sh"); OutputStream os = process.getOutputStream(); os.write(command.getBytes()); os.close(); final StringBuilder stdout = new StringBuilder(1024); final StringBuilder stderr = new StringBuilder(1024); final BufferedReader stdoutReader = new BufferedReader( new InputStreamReader(process.getInputStream(), "GBK")); final BufferedReader stderrReader = new BufferedReader( new InputStreamReader(process.getErrorStream(), "GBK")); new Thread(() -> { String line; try { while (null != (line = stdoutReader.readLine())) { stdout.append(line).append("\n"); } } catch (IOException e) { log.error("read stdout error", e); } }).start(); new Thread(() -> { String line; try { while (null != (line = stderrReader.readLine())) { stderr.append(line).append("\n"); } } catch (IOException e) { log.error("read stderr error", e); } }).start(); int ret = process.waitFor(); stdoutReader.close(); stderrReader.close(); Pair<String, String> output = Pair.of(stdout.toString(), stderr.toString()); return Pair.of(ret, output); } catch (Exception e) { return Pair.of(-1, Pair.of("", ExceptionUtils.getStackTrace(e))); } }
From source file:edu.cwru.jpdg.Dotty.java
/** * Compiles the graph to dotty./*from w w w . j av a 2 s .co m*/ */ public static String dotty(String graph) { byte[] bytes = graph.getBytes(); try { ProcessBuilder pb = new ProcessBuilder("dotty"); pb.redirectError(ProcessBuilder.Redirect.INHERIT); Process p = pb.start(); OutputStream stdin = p.getOutputStream(); stdin.write(bytes); stdin.close(); String line; BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); List<String> stdout_lines = new ArrayList<String>(); line = stdout.readLine(); while (line != null) { stdout_lines.add(line); line = stdout.readLine(); } if (p.waitFor() != 0) { throw new RuntimeException("javac failed"); } return StringUtils.join(stdout_lines, "\n"); } catch (InterruptedException e) { throw new RuntimeException(e.getMessage()); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } }
From source file:Main.java
public static boolean runRootCommand(Context context, String command) { Process process = null; DataOutputStream os = null;/*from w w w . j a v a 2 s.com*/ try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(command + "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { Log.d("*** DEBUG ***", "Error - " + e.getMessage()); return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } return true; }
From source file:Main.java
public static boolean isRooted() { Process p; try {//www . ja va 2 s . c o m p = new ProcessBuilder("su").start(); BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); stdin.write("whoami"); stdin.newLine(); stdin.write("exit"); stdin.newLine(); stdin.close(); try { p.waitFor(); if (!stdout.ready()) return false; String user = stdout.readLine(); //We only expect one line of output stdout.close(); if (user == "root") { return true; } else { return false; } } catch (InterruptedException e) { e.printStackTrace(); return false; } } catch (IOException e) { e.printStackTrace(); return false; } }
From source file:Main.java
public static boolean tempRunRootCommand(Context context, String command) { Process process = null; DataOutputStream os = null;/*from w w w . ja v a 2 s.c o m*/ try { process = Runtime.getRuntime().exec("su1"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(command + "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { Log.d("*** DEBUG ***", "Error - " + e.getMessage()); return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } return true; }
From source file:Main.java
public static boolean isRoot() { Process process = null; DataOutputStream dos = null;//from w ww. ja v a 2 s . c o m try { process = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(process.getOutputStream()); dos.writeBytes("exit\n"); dos.flush(); int exitValue = process.waitFor(); if (exitValue == 0) { return true; } } catch (IOException e) { } catch (InterruptedException e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { } } } return false; }
From source file:com.textocat.textokit.commons.io.ProcessIOUtils.java
/** * @param proc process which input stream will receive bytes from the * argument input stream/*from w ww. j a v a 2 s .c o m*/ * @param in input stream. Note that it is closed at the end. */ public static void feedProcessInput(Process proc, final InputStream in, final boolean closeStdIn) throws IOException { final OutputStream procStdIn = proc.getOutputStream(); final List<Exception> exceptions = Lists.newLinkedList(); Thread writerThread = new Thread(new Runnable() { @Override public void run() { try { IOUtils.copy(in, procStdIn); if (closeStdIn) { procStdIn.flush(); closeQuietly(procStdIn); } } catch (Exception e) { exceptions.add(e); } finally { closeQuietly(in); } } }); writerThread.start(); try { writerThread.join(); } catch (InterruptedException e) { // do nothing, just set flag Thread.currentThread().interrupt(); } if (!exceptions.isEmpty()) { Exception ex = exceptions.get(0); throw ex instanceof IOException ? (IOException) ex : new IOException("Unexpected exception in writing thread", ex); } }
From source file:minij.assembler.Assembler.java
public static void assemble(Configuration config, String assembly) throws AssemblerException { try {// w ww . jav a 2 s. c o m new File(FilenameUtils.getPath(config.outputFile)).mkdirs(); // -xc specifies the input language as C and is required for GCC to read from stdin ProcessBuilder processBuilder = new ProcessBuilder("gcc", "-o", config.outputFile, "-m32", "-xc", MiniJCompiler.RUNTIME_DIRECTORY.toString() + File.separator + "runtime_32.c", "-m32", "-xassembler", "-"); Process gccCall = processBuilder.start(); // Write C code to stdin of C Compiler OutputStream stdin = gccCall.getOutputStream(); stdin.write(assembly.getBytes()); stdin.close(); gccCall.waitFor(); // Print error messages of GCC if (gccCall.exitValue() != 0) { StringBuilder errOutput = new StringBuilder(); InputStream stderr = gccCall.getErrorStream(); String line; BufferedReader bufferedStderr = new BufferedReader(new InputStreamReader(stderr)); while ((line = bufferedStderr.readLine()) != null) { errOutput.append(line + System.lineSeparator()); } bufferedStderr.close(); stderr.close(); throw new AssemblerException( "Failed to compile assembly:" + System.lineSeparator() + errOutput.toString()); } Logger.logVerbosely("Successfully compiled assembly"); } catch (IOException e) { throw new AssemblerException("Failed to transfer assembly to gcc", e); } catch (InterruptedException e) { throw new AssemblerException("Failed to invoke gcc", e); } }
From source file:Main.java
public static String runCommand(String[] commands) { DataOutputStream outStream = null; DataInputStream responseStream; try {/*from ww w . ja v a 2 s . c o m*/ ArrayList<String> logs = new ArrayList<String>(); Process process = Runtime.getRuntime().exec("su"); Log.i(TAG, "Executed su"); outStream = new DataOutputStream(process.getOutputStream()); responseStream = new DataInputStream(process.getInputStream()); for (String single : commands) { Log.i(TAG, "Command = " + single); outStream.writeBytes(single + "\n"); outStream.flush(); if (responseStream.available() > 0) { Log.i(TAG, "Reading response"); logs.add(responseStream.readLine()); Log.i(TAG, "Read response"); } else { Log.i(TAG, "No response available"); } } outStream.writeBytes("exit\n"); outStream.flush(); String log = ""; for (int i = 0; i < logs.size(); i++) { log += logs.get(i) + "\n"; } Log.i(TAG, "Execution compeleted"); return log; } catch (IOException e) { Log.d(TAG, e.getMessage()); } return null; }
From source file:Main.java
private static boolean installOrUninstallApk(String apkPath, String installOruninstall, String rOrP) { Process process = null; DataOutputStream os = null;//from w ww.j av a 2s. c om String command = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); command = "pm " + installOruninstall + " " + rOrP + " " + apkPath + " \n"; os.writeBytes(command); os.flush(); os.close(); process.waitFor(); process.destroy(); return true; } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return false; }