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 Map<String, String> getCPUInfo() {
    Map<String, String> cpuInfo = new HashMap<String, String>();
    Runtime runtime = Runtime.getRuntime();
    try {// w  w w.  ja va 2 s. co m
        Process process = runtime.exec("cat /proc/cpuinfo");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            String[] strs = line.split(":");
            if (strs.length == 2) {
                cpuInfo.put(strs[0].trim(), strs[1].trim());
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return cpuInfo;
}

From source file:Main.java

public static BufferedReader runBufferedCommand(String cmd) throws IOException {
    Log.d(TAG, "Executing: " + cmd);
    Process process = Runtime.getRuntime().exec(cmd);
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    return reader;
}

From source file:Main.java

private static String getMacAddress() {
    String macSerial = null;//from  w w  w . j  a va  2 s. c o  m
    String str = "";
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);

        for (; null != str;) {
            str = input.readLine();
            if (str != null) {
                str = str.trim();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < str.length(); i++) {
                    if (str.charAt(i) != ':') {
                        sb.append(str.charAt(i));
                    }
                }
                macSerial = sb.toString();
                break;
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return macSerial;
}

From source file:Main.java

public static String runScript(String script) {
    String sRet;//w w w.j av a  2 s.  com
    try {
        final Process m_process = Runtime.getRuntime().exec(script);
        final StringBuilder sbread = new StringBuilder();
        Thread tout = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getInputStream()), 8192);
                String ls_1;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sbread.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tout.start();

        final StringBuilder sberr = new StringBuilder();
        Thread terr = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getErrorStream()), 8192);
                String ls_1;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sberr.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        terr.start();

        m_process.waitFor();
        while (tout.isAlive()) {
            Thread.sleep(50);
        }
        if (terr.isAlive())
            terr.interrupt();
        String stdout = sbread.toString();
        String stderr = sberr.toString();
        sRet = stdout + stderr;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return sRet;
}

From source file:Main.java

/**
 * Run a command with the given data as input.
 *//*from  w w w . j  a  va2  s .c  o m*/
public static void systemIn(String command, String data) throws Exception {
    String cmd[] = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);
    PrintWriter w = new PrintWriter(p.getOutputStream());
    w.print(data);
    w.flush();
    w.close();
    p.waitFor();
}

From source file:Main.java

/**
 * Run a command and return its output.//from  w  w w  .  ja va2s .  c om
 */
public static String systemOut(String command) throws Exception {
    int c;
    String cmd[] = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);

    BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringBuilder s = new StringBuilder();
    while ((c = r.read()) != -1)
        s.append((char) c);
    p.waitFor();

    return s.toString();
}

From source file:Main.java

public static void killPackage(final String packageToKill) {
    new AsyncTask<Void, Void, Void>() {
        @Override/* w w  w.j a  v  a2  s .com*/
        protected Void doInBackground(Void... params) {
            try {
                Runtime.getRuntime().exec(new String[] { "su", "-c", "pkill " + packageToKill });
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute();
}

From source file:Main.java

/**
 * Run a command with the given data as input.
 *//*from w  w  w  .  ja  v a2  s  . co  m*/
public static void systemIn(String command, String data) throws Exception {
    String[] cmd = new String[3];
    cmd[0] = System.getProperty("SHELL", "/bin/sh");
    cmd[1] = "-c";
    cmd[2] = command;
    Process p = Runtime.getRuntime().exec(cmd);
    PrintWriter w = new PrintWriter(p.getOutputStream());
    w.print(data);
    w.flush();
    w.close();
    p.waitFor();
}

From source file:Main.java

public static boolean isMIUI() {
    String propName = "ro.miui.ui.version.name";
    String line;/*from ww w.  j a v  a  2s.  c o m*/
    BufferedReader input = null;
    try {
        Process p = Runtime.getRuntime().exec("getprop " + propName);
        input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        return false;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            }
        }
    }
    return line != null && line.length() > 0;
}

From source file:Main.java

public static boolean execCmd(String cmd) {
    try {/*from w  w  w. ja va  2s. co m*/
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
    } catch (Exception e) {
        return false;
    }
    return true;
}