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[] commands) throws IOException, InterruptedException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    for (String cmd : commands) {
        os.writeBytes(cmd + "\n");
    }/*  w w w. ja  v a2  s .  c  o  m*/
    os.writeBytes("exit\n");
    os.flush();
    os.close();
    p.waitFor();
}

From source file:Main.java

public static boolean fileWriteOneLine(String fname, String value, Process su) {
    try {/*from   w  ww .j  a v  a2 s . co m*/
        DataOutputStream out = new DataOutputStream(su.getOutputStream());
        out.writeBytes("echo " + value + " > " + fname + "\n");
    } catch (Exception e) {
        String Error = "Error writing to " + fname + ". Exception: ";
        Log.e(TAG, Error, e);
        return false;
    }
    return true;
}

From source file:Main.java

static void runCommandWithRoot(String command) {
    try {/*w  w  w  . j a v  a  2 s .c o m*/
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        os.writeBytes(command);
        os.writeBytes("exit\n");
        os.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void RunAsRootNoOutput(String[] cmds) {
    try {//from www.j a  va  2  s  . c  o m
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void reboot() {
    try {/*  w  w w  .  jav  a  2  s  .  c  om*/
        Process su = Runtime.getRuntime().exec("su");
        OutputStreamWriter out = new OutputStreamWriter(su.getOutputStream());
        out.write("reboot");
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean restartThermald(Process su) {
    try {//from   www  . java2 s  . c  om
        DataOutputStream out = new DataOutputStream(su.getOutputStream());
        out.writeBytes("stop thermald\n");
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
        }
        out.writeBytes("start thermald\n");
    } catch (Exception e) {
        String Error = "Error restarting thermald. Exception: ";
        Log.e(TAG, Error, e);
        return false;
    }
    return true;
}

From source file:Main.java

public static int execRootCmdSilent(String cmd) {

    int result = -1;
    DataOutputStream dos = null;/*from  w ww .j  a  va 2s  .com*/

    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:Main.java

public static void execCmd(String cmd) {
    DataOutputStream dos = null;//  w  w  w.java2  s.c  om
    DataInputStream dis = null;
    try {
        Process p = Runtime.getRuntime().exec("su");
        dos = new DataOutputStream(p.getOutputStream());
        cmd += "\n";
        dos.writeBytes(cmd);
        dos.flush();
        dos.writeBytes("exit\n");
        dos.flush();
        p.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dos != null)
                dos.close();
            if (dis != null)
                dis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static List<String> execAsAdmin(String... commands) throws Exception {
    List<String> res = new ArrayList<String>();

    Process process = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(process.getOutputStream());
    for (String single : commands) {
        os.writeBytes(single + "\n");
        os.flush();/*from www .ja  va 2  s  .  co m*/
        //res.add(osRes.readLine());
    }
    os.writeBytes("exit\n");
    os.flush();
    //process.destroy();
    process.waitFor();

    return res;
}

From source file:Main.java

public static boolean symlink(File inFile, File outFile) {
    int exitCode = -1;
    try {//from www. j  av a2  s. c  om
        Process sh = Runtime.getRuntime().exec("sh");
        OutputStream out = sh.getOutputStream();
        String command = "/system/bin/ln -s " + inFile + " " + outFile + "\nexit\n";
        out.write(command.getBytes("ASCII"));

        final char buf[] = new char[40];
        InputStreamReader reader = new InputStreamReader(sh.getInputStream());
        while (reader.read(buf) != -1)
            throw new IOException("stdout: " + new String(buf));
        reader = new InputStreamReader(sh.getErrorStream());
        while (reader.read(buf) != -1)
            throw new IOException("stderr: " + new String(buf));

        exitCode = sh.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }
    return exitCode == 0;
}