Example usage for java.lang ProcessBuilder ProcessBuilder

List of usage examples for java.lang ProcessBuilder ProcessBuilder

Introduction

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

Prototype

public ProcessBuilder(String... command) 

Source Link

Document

Constructs a process builder with the specified operating system program and arguments.

Usage

From source file:Main.java

public static String runCommand(String[] command, String workdirectory) {
    String result = "";
    Log.d("AppUtil.class", "#" + command);
    try {// w  w w.j a va 2  s .  c  om
        ProcessBuilder builder = new ProcessBuilder(command);
        // set working directory
        if (workdirectory != null) {
            builder.directory(new File(workdirectory));
        }
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        byte[] buffer = new byte[1024];
        while (in.read(buffer) != -1) {
            String str = new String(buffer);
            result = result + str;
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static long getMaxCpuFreq() {
    long longRet = 0;
    String result = "0";
    ProcessBuilder cmd;/*from  w  w  w.  j  a  v  a 2 s.  com*/
    try {
        String[] args = { "/system/bin/cat", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" };
        cmd = new ProcessBuilder(args);
        Process process = cmd.start();
        InputStream in = process.getInputStream();
        byte[] re = new byte[24];
        result = "";
        while (in.read(re) != -1) {
            result = result + new String(re);
        }
        in.close();
    } catch (IOException ex) {
        ex.printStackTrace();
        result = "0";
    }

    if (result.length() != 0) {
        try {
            longRet = Long.valueOf(result.trim());
        } catch (Exception e) {
            android.util.Log.e(TAG, "");
        }
    }
    return longRet;
}

From source file:Main.java

public static long getNetSpeed() {
    ProcessBuilder cmd;// w  ww  .  j av a  2  s. c  o  m
    long readBytes = 0;
    BufferedReader rd = null;
    try {
        String[] args = { "/system/bin/cat", "/proc/net/dev" };
        cmd = new ProcessBuilder(args);
        Process process = cmd.start();
        rd = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            if (line.contains("lan0") || line.contains("eth0")) {
                String[] delim = line.split(":");
                if (delim.length >= 2) {
                    readBytes = parserNumber(delim[1].trim());
                    break;
                }
            }
        }
        rd.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (rd != null) {
            try {
                rd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return readBytes;
}

From source file:com.microsoft.alm.plugin.external.utils.ProcessHelper.java

public static Process startProcess(final String workingDirectory, final List<String> arguments)
        throws IOException {
    final ProcessBuilder pb = new ProcessBuilder(arguments);

    // Disable any telemetry that the tool may initiate
    pb.environment().put("tf_notelemetry", "TRUE");
    pb.environment().put("TF_ADDITIONAL_JAVA_ARGS", "-Duser.country=US -Duser.language=en");

    if (StringUtils.isNotEmpty(workingDirectory)) {
        pb.directory(new File(workingDirectory));
    }//from   w  w  w . j a va 2  s. c  o m
    return pb.start();
}

From source file:Main.java

public static String executeCommand(String[] args) {
    String result = new String();
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    Process process = null;/*from www . ja v a2  s.c  o m*/
    InputStream errIs = null;
    InputStream inIs = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read = -1;
        process = processBuilder.start();
        errIs = process.getErrorStream();
        while ((read = errIs.read()) != -1) {
            baos.write(read);
        }
        baos.write('\n');
        inIs = process.getInputStream();
        while ((read = inIs.read()) != -1) {
            baos.write(read);
        }
        byte[] data = baos.toByteArray();
        result = new String(data);
    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    } finally {
        try {
            if (errIs != null) {
                errIs.close();
            }
            if (inIs != null) {
                inIs.close();
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        if (process != null) {
            process.destroy();
        }
    }
    return result;
}

From source file:design.process.ProcessUtil.java

/***/
public static Process executeProcess(final File workDir, final String... termArray)
        throws IOException, InterruptedException {
    final List<String> termList = Arrays.asList(termArray);
    final ProcessBuilder builder = new ProcessBuilder(termList);
    final Process process = builder.directory(workDir).start();
    process.waitFor();//from   w  ww  .  j  a va  2s  .  c om
    return process;
}

From source file:com.ms.commons.standalone.utils.Shell.java

public static String exec(String cmd) {
    Process process = null;//  www.j a  v  a2s  . c  o m
    String[] cmds = { "/bin/bash", "-c", cmd, };
    try {
        process = new ProcessBuilder(cmds).redirectErrorStream(true).start();
        byte[] buffer = IOUtils.toByteArray(process.getInputStream());
        process.waitFor();
        return new String(buffer, "utf-8");
    } catch (Exception e) {
        logger.error("runtime.exec cmd: " + cmd + " failed", e);
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
    return "";
}

From source file:Main.java

public static String runCommand(String[] command, String workdirectory) {
    String result = "";
    //AbLogUtil.d(AbAppUtil.class, "#"+command);
    try {//from ww w .j  a  v  a2s  .  c om
        ProcessBuilder builder = new ProcessBuilder(command);
        // set working directory
        if (workdirectory != null) {
            builder.directory(new File(workdirectory));
        }
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        byte[] buffer = new byte[1024];
        while (in.read(buffer) != -1) {
            String str = new String(buffer);
            result = result + str;
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:ml.shifu.shifu.executor.ProcessManager.java

public static int runShellProcess(String currentDir, String[] args) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.directory(new File(currentDir));
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();

    LogThread logThread = new LogThread(process, process.getInputStream(), currentDir);
    logThread.start();/*from   w ww.j  a v  a2s .  co m*/

    try {
        process.waitFor();
    } catch (InterruptedException e) {
        process.destroy();
    } finally {
        logThread.setToQuit(true);
    }

    LOG.info("Under {} directory, finish run `{}`", currentDir, args);
    return process.exitValue();
}

From source file:jp.co.tis.gsp.tools.dba.util.ProcessUtil.java

public static void exec(Map<String, String> environment, String... args)
        throws IOException, InterruptedException {

    Process process = null;/*from w  w w.  j a v  a 2  s.c o m*/
    InputStream stdout = null;
    BufferedReader br = null;

    try {
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.redirectErrorStream(true);
        if (environment != null) {
            pb.environment().putAll(environment);
        }

        process = pb.start();
        stdout = process.getInputStream();
        br = new BufferedReader(new InputStreamReader(stdout));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(br);
        IOUtils.closeQuietly(stdout);

        if (process != null) {
            process.destroy();
        }
    }
}