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 void moveAllFiles(String sourceFile, String destFile) {
    if (new File(sourceFile).exists()) {
        String deleteCmd = "rename " + sourceFile + " " + destFile;
        Runtime runtime = Runtime.getRuntime();
        try {// w  ww. j  ava  2s .  co  m
            runtime.exec(deleteCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String getKernelVersion() {
    Process process = null;/*from   w w  w .  java 2s  . c om*/
    try {
        process = Runtime.getRuntime().exec("cat /proc/version");
    } catch (IOException e) {
        e.printStackTrace();
    }
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr, 8 * 1024);
    String result = "";
    String line;
    try {
        while ((line = br.readLine()) != null) {
            result += line;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (result != "") {
        String keyword = "version ";
        int index = result.indexOf(keyword);
        line = result.substring(index + keyword.length());
        index = line.indexOf(" ");
        return line.substring(0, index);
    }
    return "Unknown";
}

From source file:Main.java

public static int runSuCommandNoScriptWrapper(Context context, String command)
        throws IOException, InterruptedException {
    String[] args = new String[] { "su", "-c", command };
    Process proc = Runtime.getRuntime().exec(args);
    return proc.waitFor();
}

From source file:Main.java

private static void checkBitmapCache() {
    if (bitmapCache == null) {
        bitmapCache = new LruCache<String, Bitmap>((int) Runtime.getRuntime().maxMemory() / 8) {
            @Override/*  w  ww.  java 2s  .  com*/
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getRowBytes() * bitmap.getHeight();
            }
        };
    }
}

From source file:Main.java

public static String getSystemProperty(String propName) {
    String line = "";
    BufferedReader input = null;//from  w ww .  j  a  v a2 s  .com
    try {
        Process p = Runtime.getRuntime().exec("getprop " + propName);
        input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
        line = input.readLine();
        input.close();
    } catch (IOException ex) {
        //Log.e(TAG, "Unable to read sysprop " + propName, ex);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                //      Log.e(TAG, "Exception while closing InputStream", e);
            }
        }
    }
    return line;
}

From source file:Main.java

public static boolean copyFile(String src, String dest) {
    File test = new File(dest);
    if (!test.exists()) {
        test.mkdirs();//from w  w  w .  j  av  a  2 s .co m
    }
    Process p;
    try {
        p = Runtime.getRuntime().exec("cp " + src + " " + dest);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:Main.java

public static String getSystemProp(String key) {
    String line = "";
    try {/*from  w w  w. ja va  2s . c  om*/
        Process ifc = Runtime.getRuntime().exec("getprop " + key);
        BufferedReader bis = new BufferedReader(new InputStreamReader(ifc.getInputStream()));
        line = bis.readLine();
        ifc.destroy();

    } catch (java.io.IOException e) {
        return new String(LOGTAG);
    }

    return line;
}

From source file:Main.java

public static void remountSystemRO() throws IOException {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    os.writeBytes(CMD_REMOUNT_RO + "\n");
    os.writeBytes("exit\n");
    os.flush();/*www .  j a v  a2  s.  co  m*/

}

From source file:Main.java

public static String system(String evaluatedContents, String failedString) {
    Runtime rt = Runtime.getRuntime();
    //System.out.println("System "+evaluatedContents);
    try {//from  w  w w.j a v  a 2  s  . co  m
        Process p = rt.exec(evaluatedContents);
        InputStream istrm = p.getInputStream();
        InputStreamReader istrmrdr = new InputStreamReader(istrm);
        BufferedReader buffrdr = new BufferedReader(istrmrdr);
        String result = "";
        String data = "";
        while ((data = buffrdr.readLine()) != null) {
            result += data + "\n";
        }
        //System.out.println("Result = "+result);
        return result;
    } catch (Exception ex) {
        ex.printStackTrace();
        return failedString;

    }
}

From source file:Main.java

/**
 * Executes single ping and return/*from w w w  .ja va  2s  .co m*/
 * @param ip - address to ping
 * @return true - ping was successful
 */
public static boolean ping(String ip) {
    Runtime runtime = Runtime.getRuntime();

    try {
        Process process = runtime.exec("/system/bin/ping -c 1 " + ip);
        return process.waitFor() == 0;

    } catch (Exception ignore) {
        Log.e(TAG, "Error while", ignore);
    }

    return false;
}