root Cmd Wait - Java Native OS

Java examples for Native OS:Shell Command

Description

root Cmd Wait

Demo Code


//package com.java2s;
import java.io.DataOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] argv) throws Exception {
        String cmd = "java2s.com";
        rootCmdWait(cmd);// w  w w.j a v a  2 s .c  om
    }

    public static void rootCmdWait(String cmd) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec("/system/xbin/su");

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

            try {
                String str1 = String.valueOf(cmd);
                String str2 = str1 + "\n";
                ou.writeBytes(str2);
                ou.flush();
                ou.writeBytes("exit\n");
                ou.flush();
                p.waitFor();
                int nRet = p.exitValue();

            } catch (Exception e) {

                e.printStackTrace();
            }

        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}

Related Tutorials