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

public static int findProcessIdWithPS(String command) throws Exception {
    int procId = -1;
    Runtime r = Runtime.getRuntime();
    Process procPs = null;
    procPs = r.exec(SHELL_CMD_PS);//from   w  w  w  . ja v a 2s.c  o  m

    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 MatchResult matchSystemFile(final String pSystemFile, final String pPattern, final int pHorizon)
        throws Exception {
    InputStream in = null;//from  w  w w .  j  a  va2  s .  com
    try {
        final Process process = new ProcessBuilder(new String[] { "/system/bin/cat", pSystemFile }).start();

        in = process.getInputStream();
        final Scanner scanner = new Scanner(in);

        final boolean matchFound = scanner.findWithinHorizon(pPattern, pHorizon) != null;
        if (matchFound) {
            return scanner.match();
        } else {
            throw new Exception();
        }
    } catch (final IOException e) {
        throw new Exception(e);
    }
}

From source file:com.thoughtworks.go.util.OperatingSystem.java

private static String detectCompleteName() {
    String[] command = { "python", "-c", "import platform;print(platform.linux_distribution())" };
    try {/*from ww  w .  ja  va  2 s . c o m*/
        Process process = Runtime.getRuntime().exec(command);
        Scanner scanner = new Scanner(process.getInputStream());
        String line = scanner.nextLine();
        OS_COMPLETE_NAME = cleanUpPythonOutput(line);
    } catch (Exception e) {
        try {
            OS_COMPLETE_NAME = readFromOsRelease();
        } catch (Exception ignored) {
            OS_COMPLETE_NAME = OS_FAMILY_NAME;
        }
    }
    return OS_COMPLETE_NAME;
}

From source file:Main.java

public static boolean isAndroidEmulatorRunning(File androidSdkHome) throws IOException {
    if (androidSdkHome == null) {
        return false;
    }/* w  w w .j av  a 2s  . c o  m*/

    File adb = new File(androidSdkHome, "platform-tools" + File.separator + "adb");
    if (!adb.exists()) {
        return false;
    }

    boolean isEmulatorRunning = false;

    ProcessBuilder pb = new ProcessBuilder(adb.getAbsolutePath(), "devices");
    pb.redirectErrorStream(true);
    Process p = pb.start();

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

    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.startsWith("List of devices")) {
            continue;
        } else if (line.startsWith("emulator-")) {
            String[] tokens = line.split("\\s");
            String name = tokens[0];
            String status = tokens[1];
            int port = Integer.parseInt(name.substring(name.indexOf("-") + 1));
            if (status.equals("device") && port == 5560) {
                isEmulatorRunning = true;
            }
        }
    }

    return isEmulatorRunning;

}

From source file:Main.java

public static String dumpFile_prev(String filename) {
    String line_prev = "";
    try {//from w w  w .j a  v a  2 s .co m
        Process ifc_prev = Runtime.getRuntime().exec("cat " + filename);
        BufferedReader bis_prev = new BufferedReader(new InputStreamReader(ifc_prev.getInputStream()));
        line_prev = bis_prev.readLine();
        ifc_prev.destroy();

    } catch (java.io.IOException e) {
        return new String("");
    }

    return line_prev;
}

From source file:com.orange.clara.cloud.servicedbdumper.dbdumper.core.AbstractCoreDbAction.java

public static BufferedReader getOutput(Process p) {
    return new BufferedReader(new InputStreamReader(p.getInputStream()));
}

From source file:Main.java

public static String getMacAddress() {
    String macAddress = null;//  w w w .  j a  v  a 2s  .  co  m
    LineNumberReader reader = null;
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        reader = new LineNumberReader(ir);
        macAddress = reader.readLine().replace(":", "");
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return macAddress == null ? "" : macAddress;
}

From source file:Main.java

/**
 * create a child process with environment variables
 * @param command//from   w  w w .jav  a 2  s .c  o m
 * @param envVars
 * @return
 * @throws InterruptedException
 * @throws IOException
 */
public static String execUnixCommand(String[] command, Map<String, String> envVars)
        throws InterruptedException, IOException {

    /*
    ProcessBuilder processBuilder = new ProcessBuilder(command);
            
    if ( envVars != null ){
    Map<String, String> env = processBuilder.environment();
    env.clear();
    env.putAll(envVars);
    }
            
    Process process = processBuilder.start();
    processBuilder.redirectErrorStream(false);
    process.waitFor();
    String outputWithError = loadStream(process.getInputStream()) + loadStream(process.getErrorStream());
    return outputWithError;
     */

    ProcessBuilder processBuilder = new ProcessBuilder(command);

    if (envVars != null) {
        Map<String, String> env = processBuilder.environment();
        env.clear();
        env.putAll(envVars);
    }
    Process process = processBuilder.start();
    String output = loadStream(process.getInputStream());
    String error = loadStream(process.getErrorStream());
    process.waitFor();
    return output + "\n" + error;
}

From source file:Main.java

public static String dumpFile_prev2(String filename) {
    String line_prev2 = "";
    try {/*from  w ww. j a va2 s  .  c  o m*/
        Process ifc_prev2 = Runtime.getRuntime().exec("cat " + filename);
        BufferedReader bis_prev2 = new BufferedReader(new InputStreamReader(ifc_prev2.getInputStream()));
        line_prev2 = bis_prev2.readLine();
        ifc_prev2.destroy();

    } catch (java.io.IOException e) {
        return new String("");
    }

    return line_prev2;
}

From source file:Main.java

public static String dumpFile_spica(String filename) {
    String line_spica = "";
    try {/*from   w  w  w.  j  ava2  s.  c  o m*/
        Process ifc_spica = Runtime.getRuntime().exec("cat " + filename);
        BufferedReader bis_spica = new BufferedReader(new InputStreamReader(ifc_spica.getInputStream()));
        line_spica = bis_spica.readLine();
        ifc_spica.destroy();

    } catch (java.io.IOException e) {
        return new String("");
    }

    return line_spica;
}