Example usage for java.lang Runtime getRuntime

List of usage examples for java.lang Runtime getRuntime

Introduction

In this page you can find the example usage for java.lang Runtime getRuntime.

Prototype

public static Runtime getRuntime() 

Source Link

Document

Returns the runtime object associated with the current Java application.

Usage

From source file:Main.java

/**
 * Run command as root.//  www  . j ava2s  .  com
 * 
 * @param command
 * @return true, if command was successfully executed
 */
private static boolean runAsRoot(final String command) {
    try {

        Process pro = Runtime.getRuntime().exec("su");
        DataOutputStream outStr = new DataOutputStream(pro.getOutputStream());

        outStr.writeBytes(command);
        outStr.writeBytes("\nexit\n");
        outStr.flush();

        int retval = pro.waitFor();

        return (retval == 0);

    } catch (Exception e) {

        return false;

    }
}

From source file:Main.java

public static BufferedReader displayQRCode(String app, String email, String secret) throws IOException {
    String command = createCommand(app, email, secret);
    Process exec = Runtime.getRuntime().exec(command);
    BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
    String line;/*from   w  w w.j  a  v  a2  s.  c om*/
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
    return reader;
}

From source file:Main.java

public static String getLogcat() {
    String output = "";
    try {/*www .  j av a  2s  .  c o m*/
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                Runtime.getRuntime().exec("logcat -v -d time IconThemer *:S").getInputStream()));
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                break;
            }
            output = output + line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}

From source file:Main.java

public static String getCPUMax() {
    String txtInfo = "";
    try {/* w w w .  j a  v a2 s. co  m*/
        Process proc = Runtime.getRuntime().exec("cat /proc/cpuinfo");
        InputStream is = proc.getInputStream();
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                Log.v("TestLog", line);
                if (line.indexOf("BogoMIPS") >= 0) {
                    txtInfo = line.substring(8);
                }
            }
        } catch (IOException e) {
            // Log.v("getStringFromInputStream", "------ getStringFromInputStream " + e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }
        }
    } catch (IOException e) {
        Log.e("getCpuInfo", "------ getCpuInfo " + e.getMessage());
    }
    return txtInfo;
}

From source file:Main.java

public static int findProcessIdWithPS(String command) throws Exception {
    int procId = -1;
    Runtime r = Runtime.getRuntime();
    Process procPs = null;//from ww  w.  ja  va2s.c o  m
    procPs = r.exec(SHELL_CMD_PS);

    BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        if (line.indexOf(' ' + command) != -1) {
            StringTokenizer st = new StringTokenizer(line, " ");
            st.nextToken(); // proc owner
            procId = Integer.parseInt(st.nextToken().trim());
            break;
        }
    }

    return procId;
}

From source file:Main.java

public static boolean runRootCommand(Context context, String command) {
    Process process = null;//from  ww  w  .java  2s.c o  m
    DataOutputStream os = null;
    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 String getMac() {
    String mac = null;/*from  w  w  w . jav  a2  s.com*/
    String macSerial = null;
    String str = "";
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        //            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);

        for (; null != str;) {
            str = input.readLine();
            if (str != null) {
                macSerial = str.trim();
                break;
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    if (macSerial != null) {
        macSerial = macSerial.replace(":", "");
        macSerial = macSerial.replace("a", "A");
        macSerial = macSerial.replace("b", "B");
        macSerial = macSerial.replace("c", "C");
        macSerial = macSerial.replace("d", "D");
        macSerial = macSerial.replace("e", "E");
        mac = macSerial.replace("f", "F");
    }

    return mac;

}

From source file:Main.java

public static boolean tempRunRootCommand(Context context, String command) {
    Process process = null;/*from   w w  w  . j a va 2  s.c o m*/
    DataOutputStream os = null;
    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 void execCommand(String paramString) {
    try {/*from  ww w  .ja  va2s  .c  om*/
        String[] arrayOfString = new String[1];
        arrayOfString[0] = paramString;
        DataOutputStream localDataOutputStream = new DataOutputStream(
                Runtime.getRuntime().exec("su").getOutputStream());
        for (int i = 0;; i++) {
            if (i >= 1) {
                localDataOutputStream.writeBytes("exit\n");
                localDataOutputStream.flush();
                localDataOutputStream.wait();
                return;
            }
            localDataOutputStream.writeBytes(arrayOfString[i] + "\n");
            localDataOutputStream.flush();
        }
    } catch (Exception localException) {
    }
}

From source file:Main.java

public static String getLocalDns(String dns) {
    Process process = null;//from  ww  w . jav a 2 s .c om
    String str = "";
    BufferedReader reader = null;
    try {
        process = Runtime.getRuntime().exec("getprop net." + dns);
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = reader.readLine()) != null) {
            str += line;
        }
        reader.close();
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
            process.destroy();
        } catch (Exception e) {
        }
    }
    return str.trim();
}