Solved

Capturing output from a process

Posted on 2001-06-06
7
221 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

856 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