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

public static void execCmd(String szCmd, boolean bCatchLog) {
    try {/*from   www  .  j  av a  2 s.c o  m*/
        Log.d("PvTorrent_proc", "execCmd=" + szCmd);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(szCmd);

        if (bCatchLog) {
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while (null != (line = br.readLine())) {
                Log.e("PvTorrent_proc", line);
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("PvTorrent_proc", e.toString());
    }
}

From source file:Main.java

public static int findProcessIdWithPS(String command) throws Exception {
    int procId = -1;
    Runtime r = Runtime.getRuntime();
    Process procPs = null;//from w  ww.  j a  va 2s .  c o m
    procPs = r.exec(SHELL_CMD_PS);

    BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        if (line.indexOf(' ' + command) != -1) {
            StringTokenizer st = new StringTokenizer(line, " ");
            st.nextToken(); // proc owner
            procId = Integer.parseInt(st.nextToken().trim());
            break;
        }
    }

    return procId;
}

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  .j a v a2 s.  c  om*/
        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 void killProcess(String packageName) {
    String processId = "";
    try {//from w  w w  .ja  v a  2  s. c o m
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("ps");
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String inline;
        while ((inline = br.readLine()) != null) {
            if (packageName != null) {
                if (inline.contains(packageName)) {
                    break;
                }
            } else {
                Log.e("PvTorrent_proc", "packageName is null");
            }

        }
        br.close();
        Log.i("PvTorrent_proc", "inline" + inline);
        if (inline != null) {

            StringTokenizer processInfoTokenizer = new StringTokenizer(inline);
            int count = 0;
            while (processInfoTokenizer.hasMoreTokens()) {
                count++;
                processId = processInfoTokenizer.nextToken();
                if (count == 2) {
                    break;
                }
            }
            Log.i("PvTorrent_proc", "kill process : " + processId);
            r.exec("kill -9 " + processId);
        }
    } catch (IOException ex) {
        Log.e("PvTorrent_proc", "kill" + ex.getStackTrace());
    }
}

From source file:Main.java

public static List<String> getSDCardPaths() {
    List<String> list = new ArrayList<String>();
    BufferedReader br = null;/*from   w w  w .ja v a2s.c om*/
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("mount");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        String line;
        br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            if (line.contains("secure"))
                continue;
            if (line.contains("asec"))
                continue;

            if (line.contains("fat")) {
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    list.add(columns[1]);
                }
            } else if (line.contains("fuse")) {
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    list.add(columns[1]);
                }
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
            }
        }
    }
    return list;
}

From source file:org.jkcsoft.java.util.OsHelper.java

public static void runOsCommand(String command, Log log) throws IOException {
    try {/* w w w  .jav a2  s  .  c om*/

        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec(command);

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        // read the output from the command

        log.info("Here is the standard output of the command:");
        String stLine = null;
        while ((stLine = stdInput.readLine()) != null) {
            log.info(stLine);
        }

        // read any errors from the attempted command
        log.info("Here is the standard error of the command (if any):");
        while ((stLine = stdError.readLine()) != null) {
            log.info(stLine);
        }
    } catch (IOException e) {
        log.error("running OS command", e);
        throw e;
    }
}

From source file:Main.java

/**
 * Try to delete directory in a fast way.
 *///from  ww w.  ja v  a2 s  .  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 int findProcessIdWithPS(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs;//from www.  ja v  a 2  s .  co  m

    procPs = r.exec(SHELL_CMD_PS);

    BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
    String line;

    while ((line = reader.readLine()) != null) {
        if (line.contains(' ' + command)) {

            StringTokenizer st = new StringTokenizer(line, " ");
            st.nextToken(); // proc owner

            procId = Integer.parseInt(st.nextToken().trim());

            break;
        }
    }

    return procId;

}

From source file:biomine.bmvis2.pipeline.sources.StreamGraphSource.java

public static StreamGraphSource getNodeExpandProgramGraphSource(final String expandProgramName, VisualNode node)
        throws IOException, InterruptedException, GraphOperationException {
    String nodeId = node.getId();
    String cmd = expandProgramName + " " + nodeId;
    Logging.debug("expand", "Running command: " + cmd);
    Runtime run = Runtime.getRuntime();
    Process pr = run.exec(cmd);
    pr.waitFor();//from   w  w w .j a  v a 2 s  .  c  o  m

    StreamGraphSource s = new StreamGraphSource(pr.getInputStream(), "Neighborhood for " + nodeId);
    StreamGraphSource.assignPositions(s.getBMGraph(), node);
    return s;
}

From source file:com.crushpaper.ThreadedSynchronousStreamReader.java

/**
 * Synchronously forks a child process capturing its standard out and err
 * into StringBuffers. Returns the child process's exit code or -1 if it is
 * not available. It creates threads for each stream because that is
 * required in Java to avoid the possibility of deadlock.
 *//*ww  w .  ja  va 2  s .co  m*/
public static int exec(StringBuffer output, StringBuffer error, String args[]) {
    try {
        String osName = System.getProperty("os.name");
        if (osName.equals("Windows 95")) {
            String[] prefix = new String[2];
            prefix[0] = "command.com";
            prefix[1] = "/C";
            args = ArrayUtils.addAll(prefix, args);
        } else if (osName.startsWith("Windows")) {
            String[] prefix = new String[2];
            prefix[0] = "cmd.exe";
            prefix[1] = "/C";
            args = ArrayUtils.addAll(prefix, args);
        }

        Runtime runtime = Runtime.getRuntime();
        Process childProcess = runtime.exec(args);
        ReaderThread errorReader = new ReaderThread(childProcess.getErrorStream(), output);
        ReaderThread outputReader = new ReaderThread(childProcess.getInputStream(), error);
        errorReader.start();
        outputReader.start();
        int exitValue = childProcess.waitFor();
        return exitValue;
    } catch (Throwable t) {
        t.printStackTrace();
    }

    return -1;
}