Example usage for java.lang Process getOutputStream

List of usage examples for java.lang Process getOutputStream

Introduction

In this page you can find the example usage for java.lang Process getOutputStream.

Prototype

public abstract OutputStream getOutputStream();

Source Link

Document

Returns the output stream connected to the normal input of the process.

Usage

From source file:Main.java

public static void RunAsRoot(String string) throws IOException {
    Process P = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(P.getOutputStream());
    os.writeBytes(string + "\n");
    os.writeBytes("exit\n");
    os.flush();/*from  w  ww . ja v a2  s.com*/
}

From source file:Main.java

public static String execRootCmd(String[] cmds) {
    String result = "";
    DataOutputStream dos = null;/*from   w  ww .ja v a2 s. co  m*/
    DataInputStream dis = null;

    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        dis = new DataInputStream(p.getInputStream());

        for (String cmd : cmds) {
            Log.i("CmdUtils", cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
        }
        dos.writeBytes("exit\n");
        dos.flush();
        String line;
        while ((line = dis.readLine()) != null) {
            Log.d("result", line);
            result += line;
        }
        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:MD5.java

public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {//from   ww  w  .  j  a v  a2  s  .  c o m
        Process p = Runtime.getRuntime().exec("su");
        OutputStream os = p.getOutputStream();
        os.write(("md5sum " + recoveryFilename).getBytes());
        os.flush();
        os.close();
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str = br.readLine();
        MD5string = str.split("  ")[0].trim();
        is.close();
        br.close();
        p.destroy();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
    System.out.println(MD5string);
    return MD5string;
}

From source file:Main.java

public static void ExecuteNoReturn(String command, Boolean useroot, boolean forcenew) throws Exception {

    Process p;
    DataOutputStream os;/*  w w w.j a v  a  2  s . c o  m*/

    p = getProcess(useroot, forcenew);

    os = new DataOutputStream(p.getOutputStream());

    os.writeBytes(command + "\n");

    if (forcenew) {
        os.writeBytes("exit\n");
        os.flush();
        os.close();
    }

    //p.waitFor();
}

From source file:Main.java

public static void killProcess(int pid) {
    Process sh = null;
    DataOutputStream os = null;/* w  w w  .j a  va 2  s  .c  om*/
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "kill -9 " + pid + "\n";
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void forceStopAPK(String pkgName) {
    Process sh = null;
    DataOutputStream os = null;//from   w  w w. j a v a  2 s.c  o  m
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        final String Command = "am force-stop" + pkgName;
        os.writeBytes(Command);
        os.flush();
        sh.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Process execute(String command) throws Exception {
    final Process process = new ProcessBuilder("sh").redirectErrorStream(true).start();

    BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

    stdOutput.write(command + "; exit\n");
    stdOutput.flush();/* www . j av  a  2s  . c  om*/
    stdOutput.close();

    return process;
}

From source file:gool.executor.Command.java

/**
 * Executes a command in the specified working directory.
 * //ww  w . j a v  a  2 s . co  m
 * @param workingDir
 *            the working directory.
 * @param params
 *            the command to execute and its parameters.
 * @return the console output.
 */
public static String exec(File workingDir, List<String> params, Map<String, String> env) {
    try {
        StringBuffer buffer = new StringBuffer();

        ProcessBuilder pb = new ProcessBuilder(params);
        pb.directory(workingDir);

        for (Entry<String, String> e : env.entrySet()) {
            pb.environment().put(e.getKey(), e.getValue());
        }
        Process p = pb.redirectErrorStream(true).start();

        p.getOutputStream().close();
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line;
        while ((line = in.readLine()) != null) {
            buffer.append(line).append("\n");
        }

        int retval = p.waitFor();

        if (retval != 0) {
            throw new CommandException(
                    "The command execution returned " + retval + " as return value... !\n" + buffer);
        }

        return buffer.toString();
    } catch (IOException e) {
        throw new CommandException(e);
    } catch (InterruptedException e) {
        throw new CommandException("It seems the process was killed", e);
    }
}

From source file:Main.java

public static String sudoForResult(String... strings) {
    String res = "";
    DataOutputStream outputStream = null;
    InputStream response = null;/* ww w  . j a v  a 2  s.  com*/
    try {
        Process su = Runtime.getRuntime().exec("su");
        outputStream = new DataOutputStream(su.getOutputStream());
        response = su.getInputStream();

        for (String s : strings) {
            outputStream.writeBytes(s + "\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        res = readFully(response);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeSilently(outputStream, response);
    }
    return res;
}

From source file:Main.java

public static boolean runWithRoot(String command) {
    int result = -1;

    Process process = null;
    DataOutputStream os = null;/*from w  w  w  .j  a  va  2s .c o m*/
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(command + "\n");
        os.writeBytes("exit\n");
        os.flush();
        result = process.waitFor();
    } catch (Exception e) {
        return false;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return result == 1;
}