4 Replies Latest reply: Feb 27, 2013 8:12 PM by rp0428 RSS

    “java -version” command result in error stream

    mjmjava
      i am getting output of following command in error stream instead of input stream

      Runtime rt = Runtime.getRuntime();
                     ProcessBuilder builder = new ProcessBuilder(new String[]{"cmd.exe","/c","java -version"});
                     Process pr = builder.start();
                     BufferedReader input = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
                     String line=input.readLine();
                     System.out.println(line);

      please explain

      Edited by: Mayur Mitkari on Feb 27, 2013 5:25 AM
        • 1. Re: “java -version” command result in error stream
          UdayaShankaraGandhi
          Using System class you can work with 3 different types of streams to read or print data

          System.in : Standard Input Stream - open and ready to supply input data
          System.out : Stand Output Stream - open and ready to accept output data
          System.err : Stand Error Stream - open and ready to accept output data

          Generally output stream may be set with any output device or file. Based on the stream which are using the output will be redirected to the corresponding stream.

          Here in your program you are printing the data using "System.out" - nothing but standard output stream so what ever the output come through your program will be redirected to the default output device.

          Input streams are used to read the data. For example, assume that you want to read the data from the keyboard then you can read like the below
            
          Scanner scanner = new Scanner(System.in);
          
          while(scanner.hasNext())
          {
               System.out.println(scanner.next());
          }
          • 2. Re: “java -version” command result in error stream
            EJP
            i am getting output of following command in error stream instead of input stream
            ...
            please explain
            There's nothing to explain. If the output of the command goes to the error stream, that's where you read it from.
            • 3. Re: “java -version” command result in error stream
              baftos
              Mayur Mitkari wrote:
                             Process pr = builder.redirectErrorStream(true).start();
              This way, you will never have to worry which stream it is.
              • 4. Re: “java -version” command result in error stream
                rp0428
                >
                i am getting output of following command in error stream instead of input stream
                >
                Correct - as EJP stated.

                See for yourself.

                1. Open a command prompt
                2. execute 'java -version 2>err.txt'

                The result will be nothing on the screen and the version information in the 'err.txt' file because the '2>' redirected the error stream to the 'err.txt' file.