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

public static String exec(String cmd) {
    StringBuilder sb = new StringBuilder();
    try {/* www. j  a v a2  s. c  o m*/
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(cmd);
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
        br.close();
        if (process.waitFor() != 0) {
            System.err.println("exit value = " + process.exitValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sb.toString();
}

From source file:Main.java

public static String runAsRoot(String[] cmds) throws Exception {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    InputStream is = p.getInputStream();
    String result = null;/*from   w w  w . j ava2 s .  c o  m*/
    for (String tmpCmd : cmds) {
        os.writeBytes(tmpCmd + "\n");
        int readed = 0;
        byte[] buff = new byte[4096];
        while (is.available() <= 0) {
            try {
                Thread.sleep(5000);
            } catch (Exception ex) {
            }
        }

        while (is.available() > 0) {
            readed = is.read(buff);
            if (readed <= 0)
                break;
            String seg = new String(buff, 0, readed);
            result = seg; //result is a string to show in textview
        }
    }
    os.writeBytes("exit\n");
    os.flush();
    return result;
}

From source file:Main.java

public static void deleteAllFiles(File root) {
    if (root.exists()) {
        String deleteCmd = "rm -r " + root.getAbsolutePath();
        Runtime runtime = Runtime.getRuntime();
        try {//from   ww  w.  j a  v  a  2s . c o  m
            runtime.exec(deleteCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static int getDefaultThreadPoolSize(int max) {
    int availableProcessors = 2 * Runtime.getRuntime().availableProcessors() + 1;
    return availableProcessors > max ? max : availableProcessors;
}

From source file:Main.java

public static boolean executeAsRoot(String command) {
    try {/*  w  w  w.  ja  v a 2s  .c  o  m*/

        Process suProcess = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

        os.writeBytes(command + "\n");
        os.flush();
        Log.d("ROOT", command);

        os.writeBytes("exit\n");
        os.flush();

        try {
            int suProcessRet = suProcess.waitFor();
            if (255 != suProcessRet)
                return true;
            else
                return false;
        } catch (Exception ex) {
            Log.w("ROOT-ERROR", ex);
        }
    } catch (IOException ex) {
        Log.w("ROOT", "Can't get root access", ex);
    } catch (SecurityException ex) {
        Log.w("ROOT", "Can't get root access", ex);
    } catch (Exception ex) {
        Log.w("ROOT", "Error executing internal operation", ex);
    }
    return false;
}

From source file:Main.java

private static boolean checkRootMethod3() {
    Process process = null;//  w w  w  .ja v a  2s.  c  om
    try {
        process = Runtime.getRuntime().exec(new String[] { "/system/xbin/which", "su" });
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        return in.readLine() != null;
    } catch (Throwable t) {
        return false;
    } finally {
        if (process != null)
            process.destroy();
    }
}

From source file:Main.java

public static String getLinuxVersion() {
    try {/*from w w  w . j a va 2s .  c o  m*/
        Process process = Runtime.getRuntime().exec("cat /proc/version");
        InputStream outs = process.getInputStream();
        InputStreamReader isrout = new InputStreamReader(outs);
        BufferedReader brout = new BufferedReader(isrout, 8 * 1024);
        String result = "";
        String line;
        while ((line = brout.readLine()) != null) {
            result += line;
        }
        if (result != "") {
            String keyWord = "version ";
            int index = result.indexOf(keyWord);
            line = result.substring(index + keyWord.length());
            index = line.indexOf(" ");
            return line.substring(0, index);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static boolean isRoot() {
    Process process = null;//from  ww w.  j a  v a2 s .  co m
    DataOutputStream dos = null;
    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:Main.java

public static void changeFilePermission(File file) {
    try {//  w w w .j a  va2  s .  c  o m
        String command = "chmod 666 " + file.getAbsolutePath();
        Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

    Process process = null;//from  w  w w  .j  a v  a  2  s  . 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();
        result = process.waitFor();
    } catch (Exception e) {
        return false;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return result == 1;
}