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:com.anyi.gp.license.RegisterTools.java

public static List getMacAddresses() {
    List result = new ArrayList();
    try {/*w w  w . j ava2 s . c om*/
        Properties props = System.getProperties();
        String command = "ipconfig -a";
        if (props.getProperty("os.name").toLowerCase().startsWith("windows"))
            command = "ipconfig /all";

        Process process = Runtime.getRuntime().exec(command);
        InputStreamReader ir = new InputStreamReader(process.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);
        String line;
        while ((line = input.readLine()) != null)
            if (line.indexOf("Physical Address") > 0) {
                String MACAddr = line.substring(line.indexOf("-") - 2);
                // System.out.println("MAC address = [" + MACAddr + "]");
                result.add(MACAddr);
            }
    } catch (java.io.IOException e) {
        e.printStackTrace();
        // System.err.println("IOException " + e.getMessage());
    }
    return result;
}

From source file:Main.java

public static String getSuVersion() {
    Process process = null;
    String inLine = null;//from w  w  w.  j  a v a2s  .  c o  m

    try {
        process = Runtime.getRuntime().exec("sh");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        BufferedReader is = new BufferedReader(
                new InputStreamReader(new DataInputStream(process.getInputStream())), 64);
        os.writeBytes("su -v\n");

        // We have to hold up the thread to make sure that we're ready to read
        // the stream, using increments of 5ms makes it return as quick as
        // possible, and limiting to 1000ms makes sure that it doesn't hang for
        // too long if there's a problem.
        for (int i = 0; i < 400; i++) {
            if (is.ready()) {
                break;
            }
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                Log.w(TAG, "Sleep timer got interrupted...");
            }
        }
        if (is.ready()) {
            inLine = is.readLine();
            if (inLine != null) {
                return inLine;
            }
        } else {
            os.writeBytes("exit\n");
        }
    } catch (IOException e) {
        Log.e(TAG, "Problems reading current version.", e);
        return null;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }

    return null;
}

From source file:com.yahoo.sql4dclient.Main.java

/**
 * Run the command synchronously and return clubbed standard error and 
 * output stream's data.// w w  w  .jav a  2  s . co m
 */
private static String runCommand(final String[] cmd) throws IOException, InterruptedException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Process process = Runtime.getRuntime().exec(cmd);
    int c;
    InputStream in = process.getInputStream();
    while ((c = in.read()) != -1) {
        baos.write(c);
    }
    in = process.getErrorStream();
    while ((c = in.read()) != -1) {
        baos.write(c);
    }
    process.waitFor();
    return new String(baos.toByteArray());
}

From source file:Main.java

public static int findProcessIdWithPidOf(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs = null;

    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;/*from  ww  w .j  a v  a2 s . c om*/

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

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

    return procId;

}

From source file:com.clustercontrol.platform.PlatformPertial.java

public static void setupHostname() {
    String hostname = null;/*ww w. ja va 2 s .co m*/
    // hinemos.cfg???hostname?
    String etcDir = System.getProperty("hinemos.manager.etc.dir");
    if (etcDir != null) {
        File config = new File(etcDir, "hinemos.cfg");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(config.getAbsolutePath());
            br = new BufferedReader(fr);

            String line;
            while ((line = br.readLine()) != null) {
                line = line.trim();
                if (line.trim().startsWith("MANAGER_HOST")) {
                    hostname = line.split("=")[1];
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            log.warn("configuration file not found." + config.getAbsolutePath(), e);
        } catch (IOException e) {
            log.warn("configuration read error." + config.getAbsolutePath(), e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }

            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                }
            }
        }
    }

    if (hostname == null || hostname.length() == 0) {
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        InputStreamReader is = null;
        BufferedReader br = null;

        try {
            process = runtime.exec("hostname");

            is = new InputStreamReader(process.getInputStream());
            br = new BufferedReader(is);
            process.waitFor();

            if (br != null) {
                hostname = br.readLine();
            }

        } catch (IOException | InterruptedException e) {
            log.warn("command execute error.", e);
        } finally {
            if (process != null) {
                process.destroy();
            }

            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                }
            }

            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
    }

    if (hostname == null) {
        hostname = "";
    }

    System.setProperty("hinemos.manager.hostname", hostname);
}

From source file:com.insightml.utils.io.IoUtils.java

private static String process(final Process proc) throws IOException {
    final StringBuilder out = new StringBuilder();
    try (final BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));) {
        String s;/*from ww w  .  ja  v  a2s  .  c  o  m*/
        while ((s = stdInput.readLine()) != null) {
            out.append(s);
            out.append('\n');
        }
    } finally {
        proc.destroy();
    }
    return out.toString();
}

From source file:eu.europa.ejusticeportal.dss.applet.model.service.FileSeekerTest.java

@BeforeClass
public static void setUpTestFileStructure() throws IOException, InterruptedException {

    assertTrue(s != null && s.length() != 0);
    assertTrue(home.exists());/*  w w w. ja va 2 s . co m*/
    File testFileStructure = new File(home, "dss_applet_test/aaa/bb bb/cc ccc/ddd dd");

    if (testFileStructure.exists()) {
        FileUtils.deleteDirectory(testFileStructure);
    }
    testFileStructure = new File(home, "dss_applet_test/aaa/bb bb/cc ccc/ddd dd");
    if (!testFileStructure.exists()) {
        testFileStructure.mkdirs();
    }
    assertTrue(testFileStructure.exists());
    File library = new File(testFileStructure, "library.dll");
    if (!library.exists()) {
        library.createNewFile();
    }
    assertTrue(library.exists());

    File library2Folder = new File(home, "dss_applet_test/aaa");
    File library2 = new File(library2Folder, "pkcs11.so");
    if (!library2.exists()) {
        library2.createNewFile();
    }
    assertTrue(library2.exists());

    File library3Folder = new File(home, "dss_applet_test/aaa/bb bb/cc ccc");
    File library3 = new File(library3Folder, "pkcs11.so");
    if (!library3.exists()) {
        library3.createNewFile();
    }
    assertTrue(library3.exists());

    try {
        //Show the directory structure we created
        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "tree", "/a", "/F",
                new File(home, "dss_applet_test").getAbsolutePath());

        Process p = pb.start();
        InputStreamReader isr = new InputStreamReader(p.getInputStream(), Charset.forName("US-ASCII"));
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            log(line);
        }
    } catch (Exception e) {
        log(e.getMessage());
    }
}

From source file:Main.java

public static int findProcessIdWithPidOf(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs = null;

    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;//from   w  ww .ja  v  a 2 s. c  o m

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

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

    return procId;

}

From source file:com.moz.fiji.schema.util.JvmId.java

/**
 * Returns the Unix process ID of this JVM.
 *
 * @return the Unix process ID of this JVM.
 *///from   w ww  . ja va 2s.c  om
public static int getPid() {
    try {
        final Process process = new ProcessBuilder("/bin/sh", "-c", "echo $PPID").start();
        try {
            Preconditions.checkState(process.waitFor() == 0);
        } catch (InterruptedException ie) {
            throw new RuntimeInterruptedException(ie);
        }
        final String pidStr = IOUtils.toString(process.getInputStream()).trim();
        return Integer.parseInt(pidStr);
    } catch (IOException ioe) {
        throw new FijiIOException(ioe);
    }
}

From source file:com.diffplug.gradle.CmdLine.java

/** Runs the given command in the given directory with the given echo setting. */
public static Result runCmd(File directory, String cmd, boolean echoCmd, boolean echoOutput)
        throws IOException {
    // set the cmds
    List<String> cmds = getPlatformCmds(cmd);
    ProcessBuilder builder = new ProcessBuilder(cmds);

    // set the working directory
    builder.directory(directory);/* w  w  w.j av a2  s. c  om*/

    // execute the command
    Process process = builder.start();

    // wrap the process' input and output
    try (BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(process.getInputStream(), Charset.defaultCharset()));
            BufferedReader stdError = new BufferedReader(
                    new InputStreamReader(process.getErrorStream(), Charset.defaultCharset()));) {

        if (echoCmd) {
            System.out.println("cmd>" + cmd);
        }

        // dump the output
        ImmutableList.Builder<String> output = ImmutableList.builder();
        ImmutableList.Builder<String> error = ImmutableList.builder();

        String line = null;
        while ((line = stdInput.readLine()) != null) {
            output.add(line);
            if (echoOutput) {
                System.out.println(line);
            }
        }

        // dump the input
        while ((line = stdError.readLine()) != null) {
            error.add(line);
            if (echoOutput) {
                System.err.println(line);
            }
        }

        // check that the process exited correctly
        int exitValue = process.waitFor();
        if (exitValue != EXIT_VALUE_SUCCESS) {
            throw new RuntimeException("'" + cmd + "' exited with " + exitValue);
        }

        // returns the result of this successful execution
        return new Result(directory, cmd, output.build(), error.build());
    } catch (InterruptedException e) {
        // this isn't expected, but it's possible
        throw new RuntimeException(e);
    }
}