Solved

Capturing output from a process

Posted on 2001-06-06
7
222 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
Industry Leaders: 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 48
jboss wildfly 10.1 10 437
junit as external jar or library 7 40
Find out all repos that a user is most active on Github 1 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 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