Example usage for java.lang Process getInputStream

List of usage examples for java.lang Process getInputStream

Introduction

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

Prototype

public abstract InputStream getInputStream();

Source Link

Document

Returns the input stream connected to the normal output of the process.

Usage

From source file:Main.java

private static void killprocessNormal(String proc, int killMethod) {
    try {// www . j  av a2  s  .  co m
        ArrayList<String> pid_list = new ArrayList<String>();

        ProcessBuilder execBuilder = null;

        execBuilder = new ProcessBuilder("sh", "-c", "ps |grep " + proc);

        execBuilder.redirectErrorStream(true);

        Process exec = null;
        exec = execBuilder.start();
        InputStream is = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line = "";
        while ((line = reader.readLine()) != null) {
            String regEx = "\\s[0-9][0-9]*\\s";
            Pattern pat = Pattern.compile(regEx);
            Matcher mat = pat.matcher(line);
            if (mat.find()) {
                String temp = mat.group();
                temp = temp.replaceAll("\\s", "");
                pid_list.add(temp);
            }
        }

        for (int i = 0; i < pid_list.size(); i++) {
            execBuilder = new ProcessBuilder("su", "-c", "kill", "-" + killMethod, pid_list.get(i));
            exec = null;
            exec = execBuilder.start();

            execBuilder = new ProcessBuilder("su", "-c", "kill" + " -" + killMethod + " " + pid_list.get(i));
            exec = null;
            exec = execBuilder.start();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getCPUMaxOneCore() {
    StringBuilder sb = new StringBuilder();
    String line = null;//from  w  w  w  .java 2  s.  com

    String txtInfo = "";
    try {
        Process proc = Runtime.getRuntime().exec("cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
        InputStream is = proc.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        try {
            while ((line = br.readLine()) != null) {
                if (line.length() > 2) {
                    txtInfo = line;
                }
            }
        } catch (IOException e) {
            // Log.v("getStringFromInputStream", "------ getStringFromInputStream " + e.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    //      Log.v("getStringFromInputStream", "------ getStringFromInputStream " + e.getMessage());
                }
            }
        }

    } catch (IOException e) {

    }
    return txtInfo;
}

From source file:Main.java

public static String runAsRoot(String[] cmds) throws Exception {
    Process p = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(p.getOutputStream());
    InputStream is = p.getInputStream();
    String result = null;//  w  ww .j  a va 2  s .c  o m
    for (String tmpCmd : cmds) {
        os.writeBytes(tmpCmd + "\n");
        int readed = 0;
        byte[] buff = new byte[4096];
        while (is.available() <= 0) {
            try {
                Thread.sleep(5000);
            } catch (Exception ex) {
            }
        }

        while (is.available() > 0) {
            readed = is.read(buff);
            if (readed <= 0)
                break;
            String seg = new String(buff, 0, readed);
            result = seg; //result is a string to show in textview
        }
    }
    os.writeBytes("exit\n");
    os.flush();
    return result;
}

From source file:com.surfs.storage.common.util.CmdUtils.java

public static BufferedReader executeCmdForReader(String cmd) throws IOException {
    Process pro = Runtime.getRuntime().exec(cmd);
    BufferedReader bufReader = new BufferedReader(new InputStreamReader(pro.getInputStream(), "UTF-8"));
    return bufReader;
}

From source file:com.acmutv.ontoqa.tool.runtime.RuntimeManager.java

/**
 * Executes the given command and arguments.
 * @param command The command to execute. Arguments must be given as a separated strings.
 *                E.g.: BashExecutor.runCommand("ls", "-la") or BashExecutor.runCommand("ls", "-l", "-a")
 * @return The command output as a string.
 * @throws IOException when error in process generation or output.
 */// ww  w  .j a  va 2s.  c  om
public static String runCommand(String... command) throws IOException {
    LOGGER.trace("command={}", Arrays.asList(command));
    String out;
    ProcessBuilder pb = new ProcessBuilder(command);
    Process p = pb.start();
    try (InputStream in = p.getInputStream()) {
        out = IOUtils.toString(in, Charset.defaultCharset());
    }
    return out.trim();
}

From source file:io.bitgrillr.gocddockerexecplugin.SystemHelper.java

static String getSystemUid() throws IOException, InterruptedException {
    Process id = Runtime.getRuntime().exec(new String[] { "bash", "-c", "echo \"$(id -u):$(id -g)\"" });
    id.waitFor();//from   ww w. j  a va  2 s.  c  o  m
    return StringUtils.chomp(IOUtils.toString(id.getInputStream(), StandardCharsets.UTF_8));
}

From source file:Main.java

public static String getHardDiskSN(String drive) {
    String result = "";
    try {//from   w  ww  .  ja  va 2s  . c  om
        File file = File.createTempFile("realhowto", ".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                + "Set colDrives = objFSO.Drives\n" + "Set objDrive = colDrives.item(\"" + drive + "\")\n"
                + "Wscript.Echo objDrive.SerialNumber"; // see note
        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = input.readLine()) != null) {
            result += line;
        }
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result.trim();
}

From source file:com.surfs.storage.common.util.CmdUtils.java

public static CmdResponse executeCommand(String cmd) {
    BufferedReader bufRead = null;
    try {//from w ww.j  a v  a  2  s  . c o m
        //bufRead = executeCmdForReader(cmd);
        Process pro = Runtime.getRuntime().exec(cmd);
        bufRead = new BufferedReader(new InputStreamReader(pro.getInputStream(), "UTF-8"));
        // 0-success,others-failure
        int status = pro.waitFor();
        String response = bufRead.readLine();
        LogFactory.info("cmd:" + cmd);
        LogFactory.info("status:" + status);
        LogFactory.info("response:" + response);
        return new CmdResponse(status, response);
    } catch (Exception e) {
        return new CmdResponse(500, e.getMessage());
    } finally {
        if (bufRead != null)
            try {
                bufRead.close();
            } catch (Exception e) {
                return new CmdResponse(500, e.getMessage());
            }
    }
}

From source file:Main.java

public static String getBuildString() {

    String strValue = null;//from   w  ww .  j ava 2s. c  om
    BufferedReader birReader = null;

    try {

        Process p = Runtime.getRuntime().exec("getprop ro.modversion");
        birReader = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
        strValue = birReader.readLine();
        birReader.close();

    } catch (IOException e) {
        Log.e("HelperFunctions", "Unable to read property", e);
    } finally {

        if (birReader != null) {

            try {

                birReader.close();

            } catch (IOException e) {
                Log.e("HelperFunctions", "Exception while closing the file", e);
            }

        }

    }

    return strValue;

}

From source file:com.alibaba.jstorm.utils.SystemOperation.java

public static String exec(String cmd) throws IOException {
    LOG.debug("Shell cmd: " + cmd);
    Process process = new ProcessBuilder(new String[] { "/bin/bash", "-c", cmd }).start();
    try {//from  ww w . ja v a 2s.c om
        process.waitFor();
        String output = IOUtils.toString(process.getInputStream());
        String errorOutput = IOUtils.toString(process.getErrorStream());
        LOG.debug("Shell Output: " + output);
        if (errorOutput.length() != 0) {
            LOG.error("Shell Error Output: " + errorOutput);
            throw new IOException(errorOutput);
        }
        return output;
    } catch (InterruptedException ie) {
        throw new IOException(ie.toString());
    }

}