Solved

Capturing output from a process

Posted on 2001-06-06
7
216 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:chrisos
Comment Utility
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
Comment Utility
Don't you think 25 points is a bit on the small side?
0
 
LVL 1

Author Comment

by:nixj14
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now