понедельник, 25 апреля 2011 г.

Java: how to test, run or not some process in your system?

public static boolean isRunning(String procName) {
    boolean found=false;
    String command="";
    if (System.getProperty("os.name").contains("Windows")) {
        command="tasklist";
    } else {
        command="ps ax";
    }
    try {
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(command);
        p.waitFor();
        BufferedReader br = new BufferedReader (new InputStreamReader (p.getInputStream()));
        while (br.ready()) {
            if (br.readLine().contains(procName)) 
                 found=true;
        }
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        return found;
    }
}

Комментариев нет:

Отправить комментарий