Example usage for java.lang Runtime exec

List of usage examples for java.lang Runtime exec

Introduction

In this page you can find the example usage for java.lang Runtime exec.

Prototype

public Process exec(String cmdarray[]) throws IOException 

Source Link

Document

Executes the specified command and arguments in a separate process.

Usage

From source file:Main.java

private static int exec(String cmd) {
    try {/*  w  w w  .  j  a  v a  2 s .com*/
        Runtime runtime = Runtime.getRuntime();
        Process p = runtime.exec(cmd);
        return p.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
        return -1;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:Main.java

public static void chmod(String mode, String path) {
    try {//  w  w  w . jav a 2s . c  om
        String command = "chmod " + mode + " " + path;
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

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 {//from  w w  w .  j av  a2 s.co m
            runtime.exec(deleteCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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 w ww  . j  a va2s.c om*/
            runtime.exec(deleteCmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String getSDRoot() {
    String pre = null;/* w  ww.j  a va  2s .c  om*/
    String root = null;
    try {
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("ls mnt");
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inline;
        while ((inline = br.readLine()) != null) {
            if (inline.contains("mmcblk")) {
                pre = inline;
                break;
            }
        }
        br.close();
        root = "/mnt/" + pre + "/" + pre + "p1";
    } catch (Exception e) {

    }
    return root;
}

From source file:Main.java

public static void chmod(String permission, String path) {
    try {//from w ww  .j a va 2s. c  o m
        String command = "chmod " + permission + " " + path;
        Runtime runtime = Runtime.getRuntime();
        java.lang.Process process = runtime.exec(command);
        process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Process execRuntimeProcess(String cmd) {
    Runtime localRuntime = Runtime.getRuntime();
    try {/*from   w w w . j a va  2  s. c  o m*/
        Process pro = localRuntime.exec("su");
        DataOutputStream out = new DataOutputStream(pro.getOutputStream());
        out.writeBytes(cmd + " \n");
        return pro;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String exec(String cmd) {
    StringBuilder sb = new StringBuilder();
    try {/*w w w.j  ava 2s  .  c om*/
        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 boolean isOnline() {
    Runtime runtime = Runtime.getRuntime();
    try {/* w  w  w. j  a  v a2 s .  c om*/
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * Executes single ping and return/*w  ww  .j a  v  a  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;
}