Solved

Capturing output from a process

Posted on 2001-06-06
7
223 Views
Last Modified: 2010-04-16
I'm currently trying to capture the output of a given command so I can run some cross-checking with the results (the output).  Here's what I'm using now, and it doesn't work.

Process p = Runtime.getRuntime().exec("java -version");
java.io.BufferedReader br = new  java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String line = null;
for(line = br.readLine();line!=null;)
{
     System.out.println(line);
     line = br.readLine();
}

This is not working.  So does anybody know why?
0
Comment
Question by:nixj14
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:Droby10
ID: 6159494
hmmmm....don't know, it looks logical...you might try

Process p = Runtime.getRuntime().exec("java -version");
java.io.BufferedReader br = new  java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
String line = br.readLine();
while ( line!=null ) {
    System.out.println(line);
    line = br.readLine();
}
0
 
LVL 1

Author Comment

by:nixj14
ID: 6159511
Nope, I still don't get any output.  Its as if the java command is hiding the output from the process if thats even possible.
0
 
LVL 5

Accepted Solution

by:
Calron earned 25 total points
ID: 6159544
Try reading from the Error stream.

p.getErrorStream()

I'm not sure why in this case the result is written into the error stream, that probably depends on the application that you are calling. Just read both of them in and print both of them out.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:chrisos
ID: 6159578
Try this for size.  It runs an external ping on an Win2000 machine in this case, and retrieves the results realtime.

Regards,

Chrisos

//////////////////////////////////////////////////////////

package com.wintermuteis;

import java.io.*;

public class RuntimeExample extends Object {

     public static InputStream run(String command, String[] typedArguments, String commandSeparator) {
          Runtime runtime = null;
          Process process = null;
          BufferedInputStream myStandardOutput = null;
          OutputStreamWriter myStringInput = null;
          BufferedOutputStream myInput = null;

          try {
               runtime = Runtime.getRuntime();
               process = runtime.exec(command);
               myInput = new BufferedOutputStream(process.getOutputStream());
               myStringInput = new OutputStreamWriter(myInput);
               myStandardOutput = new BufferedInputStream(process.getInputStream());
               if( typedArguments != null && commandSeparator != null ) {
                    for( int i = 0; i < typedArguments.length; i++ ) {
                         myStringInput.write(typedArguments[i]);
                         myStringInput.write(commandSeparator);
                         myStringInput.flush();
                         myInput.flush();
                    }
               }
               //myStringInput.close();
               //myInput.close();
          }
          catch( IOException ioe ) {
               ioe.printStackTrace();
          }
          return myStandardOutput;
     }

     public static void displayOutput( InputStream processOutput, String terminateSequence ) {
          try {
               InputStreamReader niceProcessOutput = new InputStreamReader(processOutput);
               StringBuffer display = new StringBuffer();
               String output = null;
               boolean complete = false;
               char[] chars = new char[1024];
               int received = 0;

               while( !complete ) {
                    if( niceProcessOutput.ready() ) {
                         display.setLength(0);
                         received = chars.length;
                         while( received == chars.length ) {
                              received = niceProcessOutput.read(chars, 0, chars.length);
                              display.append(chars, 0, received);
                         }
                         output = display.toString();
                         if( output.indexOf(terminateSequence) > -1 ) {
                              complete = true;
                         }
                         if( output.length() > 0 ) {
                              if( output.indexOf("\n") > -1 ) {
                                   System.out.print(output);
                              }
                         }
                         try {
                              Thread.sleep(500);
                         }
                         catch( InterruptedException ie ) {
                         }
                    }
               }
          }
          catch( IOException ioe ) {
               ioe.printStackTrace();
          }
     }

     public static void main( String[] args ) {
          System.out.println("Start");
          InputStream processOutput = null;
          String[] typedCommands = null;

          processOutput = run("ping www.experts-exchange.com -n 10 -w 2000", null, "\r");
          displayOutput(processOutput, "Average");

          System.out.println("Finish");
     }
}
0
 
LVL 3

Expert Comment

by:chrisos
ID: 6159588
Don't you think 25 points is a bit on the small side?
0
 
LVL 1

Author Comment

by:nixj14
ID: 6159692
Thanks, that solved it.  I'm assuming that whoever developed that command line switch used System.err istead of System.out.  I appreciate the help!
0
 
LVL 5

Expert Comment

by:Droby10
ID: 6159709
actually i think calron hit it...i tried it with the errorstream and it worked...also

from the command:
if i run java -version > j.version
i still get the printout with nothing in the file

if i run java -version 2> j.version
the printout is empty and the file contains the data.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question