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

private static String getAaptResult(String sdkPath, String apkPath, final Pattern pattern) {
    try {//w  w w .j  a  va 2 s .c  om
        final File apkFile = new File(apkPath);
        final ByteArrayOutputStream aaptOutput = new ByteArrayOutputStream();
        final String command = getAaptDumpBadgingCommand(sdkPath, apkFile.getName());

        Process process = Runtime.getRuntime().exec(command, null, apkFile.getParentFile());

        InputStream inputStream = process.getInputStream();
        for (int last = inputStream.read(); last != -1; last = inputStream.read()) {
            aaptOutput.write(last);
        }

        String packageId = "";
        final String aaptResult = aaptOutput.toString();
        if (aaptResult.length() > 0) {
            final Matcher matcher = pattern.matcher(aaptResult);
            if (matcher.find()) {
                packageId = matcher.group(1);
            }
        }
        return packageId;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

@SuppressLint("HardwareIds")
static String getPhoneMacAddress(Context context) {
    String mac = "";

    InputStreamReader inputStreamReader = null;
    LineNumberReader lineNumberReader = null;
    try {//from w ww . jav  a 2  s.  c o  m
        @SuppressLint("WifiManagerPotentialLeak")
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        mac = info.getMacAddress();

        if (TextUtils.isEmpty(mac)) {
            Process process = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
            inputStreamReader = new InputStreamReader(process.getInputStream());
            lineNumberReader = new LineNumberReader(inputStreamReader);
            String line = lineNumberReader.readLine();
            if (line != null) {
                mac = line.trim();
            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (lineNumberReader != null) {
            try {
                lineNumberReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    if (TextUtils.isEmpty(mac)) {
        mac = "na";
    }

    return mac;
}

From source file:nl.dreamkernel.s4.tweaker.util.RuntimeExec.java

public static String[] execute(String[] commands, boolean needResponce) {
    try {// w  w  w.j  a v a2s  . c o  m
        Process process = Runtime.getRuntime().exec(commands);
        if (needResponce) {
            DataInputStream inputStream = new DataInputStream(process.getInputStream());
            if (inputStream != null) {
                String ret = "";
                int size = 0;
                byte[] buffer = new byte[1024];
                try {
                    do {
                        size = inputStream.read(buffer);
                        if (size > 0) {
                            ret += new String(buffer, 0, size, HTTP.UTF_8);
                        }
                    } while (inputStream.available() > 0);
                } catch (IOException e) {
                }
                return ret.split("\n");
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int threadPoolSize(int minimalSize) {
    int threadPoolSize = Runtime.getRuntime().availableProcessors() * 2;
    if (threadPoolSize < minimalSize)
        threadPoolSize = minimalSize;//  w  w w  .jav a  2  s .  co m
    return threadPoolSize;
}

From source file:Main.java

public static String runScript(String script) {
    String sRet = "";
    try {/* w w  w.j a  v a  2s  .  c  om*/
        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 = null;
                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 = null;
                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();

        // int retvalue = 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:io.bitgrillr.gocddockerexecplugin.SystemHelper.java

static String getSystemUid() throws IOException, InterruptedException {
    Process id = Runtime.getRuntime().exec(new String[] { "bash", "-c", "echo \"$(id -u):$(id -g)\"" });
    id.waitFor();//  w w w  .  j a va  2s. com
    return StringUtils.chomp(IOUtils.toString(id.getInputStream(), StandardCharsets.UTF_8));
}

From source file:Main.java

public static void addExecutorShutdownHook(final ExecutorService exec, final long time,
        final TimeUnit timeUnit) {
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        public void run() {
            exec.shutdownNow();//from   ww w.jav a2 s. c  om
        }
    }));
}

From source file:Util.java

public static String[] getMemoryInfo() {
    double max = Runtime.getRuntime().maxMemory() / 1024; // maxMemory is the upper limit the jvm can use
    double allocated = Runtime.getRuntime().totalMemory() / 1024; // totalMemory the size of the current allocation
    // pool/*from www.j av a  2s  . c  om*/
    double nonAllocated = max - allocated; // non allocated memory till jvm limit
    double cached = Runtime.getRuntime().freeMemory() / 1024; // freeMemory the unused memory in the allocation pool
    double used = allocated - cached; // really used memory
    double useable = max - used; // allocated, but non-used and non-allocated memory
    DecimalFormat df = new DecimalFormat(" (0.0000'%')");
    DecimalFormat df2 = new DecimalFormat(" # 'KB'");
    return new String[] { //
            "+----", //
            "| Global Memory Informations at " + getRealTime().toString() + ":", //
            "|    |", //
            "| Allowed Memory:" + df2.format(max), //
            "|    |= Allocated Memory:" + df2.format(allocated) + df.format(allocated / max * 100), //
            "|    |= Non-Allocated Memory:" + df2.format(nonAllocated) + df.format(nonAllocated / max * 100), //
            "| Allocated Memory:" + df2.format(allocated), //
            "|    |= Used Memory:" + df2.format(used) + df.format(used / max * 100), //
            "|    |= Unused (cached) Memory:" + df2.format(cached) + df.format(cached / max * 100), //
            "| Useable Memory:" + df2.format(useable) + df.format(useable / max * 100), //
            "+----" //
    };
}

From source file:Main.java

public static String runScript(String script) {
    String sRet = "";
    try {/*ww w .j a  v a2  s  . c om*/
        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 = null;
                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 = null;
                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();

        int retvalue = 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

/**
 * @see Runtime#addShutdownHook(Thread)
 */
public static void addShutdownHook(Thread hook) {
    Runtime.getRuntime().addShutdownHook(hook);
}