Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Capturing output from a process

Posted on 2001-06-06
7
Medium Priority
?
227 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 100 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

715 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