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:MiscUtils.java

public static void openBrowser(String url) throws IOException {
    final String os = System.getProperty("os.name").toLowerCase();
    if (os.indexOf("windows") != -1) {
        url = appendUrlForWindows2000(os, url);
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        return;/* w  ww  . j av  a 2  s  .  co m*/
    } else if (os.indexOf("mac") != -1) {
        Runtime.getRuntime().exec(new String[] { "open", url });
        return;
    } else {
        Runtime runtime = Runtime.getRuntime();
        for (int i = 0; i < browserNames.length; i++) {
            try {
                runtime.exec(new String[] { browserNames[i], url });
                return;
            } catch (Exception e) {
            }
        }
    }

}

From source file:com.manydesigns.elements.util.ElementsFileUtils.java

public static boolean chmod(File file, String perms) {
    logger.debug("chmod {} {}", perms, file.getAbsolutePath());
    Runtime runtime = Runtime.getRuntime();
    try {/*from  ww w .j  a v  a 2 s  .  co m*/
        Process process = runtime.exec(new String[] { "chmod", perms, file.getAbsolutePath() });
        int result = process.waitFor();
        return result == 0;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static int findProcessIdWithPidOf(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs = null;/*w  w  w . ja  v  a2s.c o m*/

    String baseName = new File(command).getName();
    //fix contributed my mikos on 2010.12.10
    procPs = r.exec(new String[] { SHELL_CMD_PIDOF, baseName });
    //procPs = r.exec(SHELL_CMD_PIDOF);

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

    while ((line = reader.readLine()) != null) {

        try {
            //this line should just be the process id
            procId = Integer.parseInt(line.trim());
            break;
        } catch (NumberFormatException e) {
            logException("unable to parse process pid: " + line, e);
        }
    }

    return procId;

}

From source file:com.zenome.bundlebus.Util.java

public static void removeFileFolderRecursive(@NonNull File aPath) throws IOException {
    if (aPath.exists()) {
        String deleteCmd = "rm -r " + aPath.getAbsolutePath();
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(deleteCmd);
    }/* w  ww  . j  ava  2  s . com*/
}

From source file:com.googlecode.jsendnsca.MessagePayloadTest.java

private static String getShortHostNameFromOS() throws Exception {
    final Runtime runtime = Runtime.getRuntime();
    final Process process = runtime.exec("hostname");
    final BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));

    final String expectedHostName = input.readLine();
    input.close();/*w w  w .j  ava 2s .c  o m*/
    assertEquals(0, process.waitFor());

    return expectedHostName;
}

From source file:com.zenome.bundlebus.Util.java

public static void copyFileFolder(@NonNull String aSourcePath, @NonNull String aTargetPath) throws IOException {
    File source = new File(aSourcePath);
    if (source.exists()) {
        String cpCmd = "cp -R " + aSourcePath + " " + aTargetPath;
        Log.d(TAG, cpCmd);/*  w w  w .java2 s . co m*/
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(cpCmd);
    }
}

From source file:com.zenome.bundlebus.Util.java

public static void moveFileFolder(@NonNull String aSourceFolderPath, @NonNull String aTargetFolderPath)
        throws IOException {
    File source = new File(aSourceFolderPath);
    if (source.exists()) {
        String moveCmd = "mv " + aSourceFolderPath + " " + aTargetFolderPath;
        Log.d(TAG, moveCmd);/*from   ww w .j  av a2s .  co  m*/
        Runtime runtime = Runtime.getRuntime();
        runtime.exec(moveCmd);
    }
}

From source file:org.apache.stratos.common.util.CommandUtils.java

public static String executeCommand(String command) throws IOException {
    String line;//from  ww  w.j a  va 2 s  .c o  m
    Runtime r = Runtime.getRuntime();
    if (log.isDebugEnabled()) {
        log.debug("command = " + command);
    }
    Process p = r.exec(command);

    StringBuilder output = new StringBuilder();
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = in.readLine()) != null) {
        if (log.isDebugEnabled()) {
            log.debug("output = " + line);
        }
        output.append(line).append(NEW_LINE);
    }
    StringBuilder errors = new StringBuilder();
    BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    while ((line = error.readLine()) != null) {
        if (log.isDebugEnabled()) {
            log.debug("error = " + line);
        }
        errors.append(line).append(NEW_LINE);
    }
    if (errors.length() > 0) {
        throw new RuntimeException("Command execution failed: " + NEW_LINE + errors.toString());
    }

    return output.toString();
}

From source file:net.pickapack.io.cmd.CommandLineHelper.java

/**
 *
 * @param cmd/*from  ww w .  j  a  va2 s .c  o  m*/
 * @return
 */
public static List<String> invokeNativeCommandAndGetResult(String[] cmd) {
    List<String> outputList = new ArrayList<String>();

    try {
        Runtime r = Runtime.getRuntime();
        Process ps = r.exec(cmd);
        //            ProcessBuilder pb = new ProcessBuilder(cmd);
        //            pb.redirectErrorStream(true);
        //            Process ps = pb.start();

        BufferedReader rdr = new BufferedReader(new InputStreamReader(ps.getInputStream()));
        String in = rdr.readLine();
        while (in != null) {
            outputList.add(in);
            in = rdr.readLine();
        }

        int exitValue = ps.waitFor();
        if (exitValue != 0) {
            System.out.println("WARN: Process exits with non-zero code: " + exitValue);
        }

        ps.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return outputList;
}

From source file:theofilin.CameraApps.java

public static void GrabCamera(JLabel tampil, int width, int height) {

    try {//from  w w  w .  ja  va  2s  . c om
        Runtime rt = Runtime.getRuntime();
        String command = "raspistill -n -t 1 -drc high -ISO 200 " + "-w " + width + " -h " + height + " -o -";
        Process p = rt.exec(command);

        //tampilin di label dulu

        InputStream is = p.getInputStream();
        byte[] curr = IOUtils.toByteArray(is);
        ImageIcon disp = new ImageIcon(curr);
        tampil.setIcon(disp);

        //reset InputStream
        is = null;

        //tulis difile kemudian
        is = new ByteArrayInputStream(curr);
        img = ImageIO.read(is);

        impl = new ImagePlus(null, img);
        ip = impl.getProcessor();
    } catch (IOException ex) {
        Logger.getLogger(CameraApps.class.getName()).log(Level.SEVERE, null, ex);
    }
}