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 runCommand(String[] commands) {
    DataOutputStream outStream = null;
    DataInputStream responseStream;
    try {/*from  w ww.  jav  a2 s.co m*/
        ArrayList<String> logs = new ArrayList<String>();
        Process process = Runtime.getRuntime().exec("su");
        Log.i(TAG, "Executed su");
        outStream = new DataOutputStream(process.getOutputStream());
        responseStream = new DataInputStream(process.getInputStream());

        for (String single : commands) {
            Log.i(TAG, "Command = " + single);
            outStream.writeBytes(single + "\n");
            outStream.flush();
            if (responseStream.available() > 0) {
                Log.i(TAG, "Reading response");
                logs.add(responseStream.readLine());
                Log.i(TAG, "Read response");
            } else {
                Log.i(TAG, "No response available");
            }
        }
        outStream.writeBytes("exit\n");
        outStream.flush();
        String log = "";
        for (int i = 0; i < logs.size(); i++) {
            log += logs.get(i) + "\n";
        }
        Log.i(TAG, "Execution compeleted");
        return log;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
    }
    return null;
}

From source file:Main.java

public static String do_exec_with_root(String cmd) {
    String s = "\n";
    try {//from w  w w .  jav  a 2 s .  co m
        Process su_p = Runtime.getRuntime().exec("su");
        DataOutputStream dataOutputStream = new DataOutputStream(su_p.getOutputStream());
        dataOutputStream.writeBytes(cmd + "\n");
        dataOutputStream.writeBytes("exit" + "\n");
        dataOutputStream.flush();
        BufferedReader in = new BufferedReader(new InputStreamReader(su_p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            s += line + "\n";
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

/**
 * Try to delete directory in a fast way.
 *//*from w ww .  jav  a 2s .  c o  m*/
public static void deleteDirectoryQuickly(File dir) throws IOException {

    if (!dir.exists()) {
        return;
    }
    final File to = new File(dir.getAbsolutePath() + System.currentTimeMillis());
    dir.renameTo(to);
    if (!dir.exists()) {
        // rebuild
        dir.mkdirs();
    }

    // try to run "rm -r" to remove the whole directory
    if (to.exists()) {
        String deleteCmd = "rm -r " + to;
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec(deleteCmd);
            process.waitFor();
        } catch (IOException e) {

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if (!to.exists()) {
        return;
    }
    deleteDirectoryRecursively(to);
    if (to.exists()) {
        to.delete();
    }
}

From source file:Main.java

public static String getMacAddress() {
    String macAddress = null;/*w w  w.  j  a  v  a  2 s .  c  o m*/
    LineNumberReader reader = null;
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        reader = new LineNumberReader(ir);
        macAddress = reader.readLine().replace(":", "");
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return macAddress == null ? "" : macAddress;
}

From source file:Main.java

public static String getMemInfo() {
    return "totalMB:" + Runtime.getRuntime().totalMemory() / MB + ", usedMB:"
            + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / MB;
}

From source file:com.surfs.storage.common.util.CmdUtils.java

public static CmdResponse executeCommand(String cmd) {
    BufferedReader bufRead = null;
    try {/*from  w ww. ja  v  a 2 s . co  m*/
        //bufRead = executeCmdForReader(cmd);
        Process pro = Runtime.getRuntime().exec(cmd);
        bufRead = new BufferedReader(new InputStreamReader(pro.getInputStream(), "UTF-8"));
        // 0-success,others-failure
        int status = pro.waitFor();
        String response = bufRead.readLine();
        LogFactory.info("cmd:" + cmd);
        LogFactory.info("status:" + status);
        LogFactory.info("response:" + response);
        return new CmdResponse(status, response);
    } catch (Exception e) {
        return new CmdResponse(500, e.getMessage());
    } finally {
        if (bufRead != null)
            try {
                bufRead.close();
            } catch (Exception e) {
                return new CmdResponse(500, e.getMessage());
            }
    }
}

From source file:Main.java

public static String getHardDiskSN(String drive) {
    String result = "";
    try {/*from   w  w w  . ja v a  2  s. co m*/
        File file = File.createTempFile("realhowto", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                + "Set colDrives = objFSO.Drives\n" + "Set objDrive = colDrives.item(\"" + drive + "\")\n"
                + "Wscript.Echo objDrive.SerialNumber"; // see note
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            result += line;
        }
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result.trim();
}

From source file:Main.java

public static long availableMemory() {
    final Runtime runtime = Runtime.getRuntime();
    final long used = runtime.totalMemory() - runtime.freeMemory();

    final ActivityManager activityManager = (ActivityManager) sContext
            .getSystemService(Context.ACTIVITY_SERVICE);
    final long total = activityManager.getMemoryClass() * 1024 * 1024;

    return total - used;
}

From source file:Main.java

public static long getMemoryMax() {
    return Runtime.getRuntime().maxMemory();
}

From source file:Main.java

public static long getMemoryTotal() {
    return Runtime.getRuntime().totalMemory();
}