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:pccontrol.PcControl.java

/**
 * Fecha navegadores do windows/*from   www. j av a  2s  . com*/
 * @throws IOException 
 */
private static void closeWindowsBrowsers() throws IOException {
    Runtime rt = Runtime.getRuntime();
    rt.exec("Taskkill /F /IM chrome.exe");
    rt.exec("Taskkill /F /IM firefox.exe");
    rt.exec("Taskkill /F /IM iexplore.exe");
    rt.exec("Taskkill /F /IM opera.exe");
    rt.exec("Taskkill /F /IM safari.exe");
}

From source file:Main.java

public static String system(String evaluatedContents, String failedString) {
    Runtime rt = Runtime.getRuntime();
    //System.out.println("System "+evaluatedContents);
    try {//from w w  w .  j  av  a2s .c om
        Process p = rt.exec(evaluatedContents);
        InputStream istrm = p.getInputStream();
        InputStreamReader istrmrdr = new InputStreamReader(istrm);
        BufferedReader buffrdr = new BufferedReader(istrmrdr);
        String result = "";
        String data = "";
        while ((data = buffrdr.readLine()) != null) {
            result += data + "\n";
        }
        //System.out.println("Result = "+result);
        return result;
    } catch (Exception ex) {
        ex.printStackTrace();
        return failedString;

    }
}

From source file:Main.java

public static List<String> getExtSDCardPaths() {
    List<String> paths = new ArrayList<String>();
    String extFileStatus = Environment.getExternalStorageState();
    File extFile = Environment.getExternalStorageDirectory();
    if (extFileStatus.endsWith(Environment.MEDIA_UNMOUNTED) && extFile.exists() && extFile.isDirectory()
            && extFile.canWrite()) {
        paths.add(extFile.getAbsolutePath());
    }//  w  w  w . jav a  2s  .c om
    try {
        // obtain executed result of command line code of 'mount', to judge
        // whether tfCard exists by the result
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("mount");
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        int mountPathIndex = 1;
        while ((line = br.readLine()) != null) {
            // format of sdcard file system: vfat/fuse
            if ((!line.contains("fat") && !line.contains("fuse") && !line.contains("storage"))
                    || line.contains("secure") || line.contains("asec") || line.contains("firmware")
                    || line.contains("shell") || line.contains("obb") || line.contains("legacy")
                    || line.contains("data")) {
                continue;
            }
            String[] parts = line.split(" ");
            int length = parts.length;
            if (mountPathIndex >= length) {
                continue;
            }
            String mountPath = parts[mountPathIndex];
            if (!mountPath.contains("/") || mountPath.contains("data") || mountPath.contains("Data")) {
                continue;
            }
            File mountRoot = new File(mountPath);
            if (!mountRoot.exists() || !mountRoot.isDirectory() || !mountRoot.canWrite()) {
                continue;
            }
            boolean equalsToPrimarySD = mountPath.equals(extFile.getAbsolutePath());
            if (equalsToPrimarySD) {
                continue;
            }
            paths.add(mountPath);
        }
    } catch (IOException e) {
        Log.e(TAG, "IOException:" + e.getMessage());
    }
    return paths;
}

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  w w . j ava  2 s  . 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 int findProcessIdWithPS(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs = null;//  w w w .  j a  v  a 2  s . 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:org.clickframes.graph.GraphUtils.java

public static boolean isGraphingAvailable(GraphingEngine graphingEngine) {
    String[] cmd = new String[] {
            // "\"" + graphingEngine.getCommand() + "\"", "-V" };
            "" + graphingEngine.getCommand() + "", "-V" };

    Process p;/*from  w  ww  .  j av  a  2 s .co m*/
    try {
        Runtime runtime = Runtime.getRuntime();
        p = runtime.exec(cmd);
        p.waitFor();

        return true;
    } catch (IOException e) {
        log.error("Exception while executing " + DOT + ", not installed? " + e.getMessage(), e);
        return false;
    } catch (InterruptedException e) {
        return false;
    }

}

From source file:org.ut.biolab.medsavant.shared.util.DirectorySettings.java

public static File getDatabaseWorkingDir() throws IOException {
    File f = new File(getTmpDirectory(), "database_work");
    if (!f.exists()) {
        f.mkdirs();/*from  ww w  .j a v a2  s.  co m*/
    }
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(CHMOD + " -R 777 " + f.getAbsolutePath());
    try {
        proc.waitFor();
    } catch (Exception ex) {
        LOG.error("Can't chmod on database working dir ", ex);
    }
    return f;

}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method to execute command arrays./*from   w  ww  .  j  a  va  2s. c om*/
 * 
 * @param command
 * @return
 * @throws IOException
 * @throws InterruptedException
 */
public static Result executeCommand(String... command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:com.impetus.ankush.agent.utils.CommandExecutor.java

/**
 * Method executeCommand./* w  ww  .j a  va  2s. c o m*/
 * 
 * @param command
 *            String
 * @return Result
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws InterruptedException
 *             the interrupted exception
 */
public static Result executeCommand(String command) throws IOException, InterruptedException {
    Result rs = new Result();
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    StreamWrapper error = new StreamWrapper(proc.getErrorStream());
    StreamWrapper output = new StreamWrapper(proc.getInputStream());

    error.start();
    output.start();
    error.join(SLEEP_TIME);
    output.join(SLEEP_TIME);
    proc.waitFor();
    rs.setExitVal(proc.exitValue());
    rs.setOutput(output.getMessage());
    rs.setError(error.getMessage());
    proc.destroy();
    return rs;
}

From source file:com.highcharts.export.util.SVGCreator.java

public static int executeCommandLine(final String commandLine, final long timeout)
        throws IOException, InterruptedException, TimeoutException {
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(commandLine);

    Worker worker = new Worker(process);
    worker.start();//from   www .  j  a  v  a  2  s  .  com
    try {
        worker.join(timeout);
        if (worker.exit != null)
            return worker.exit;
        else
            throw new TimeoutException();
    } catch (InterruptedException ex) {
        worker.interrupt();
        Thread.currentThread().interrupt();
        throw ex;
    } finally {
        process.destroy();
    }

}