0

I am calling jar in a java program. the inner jar returns some output. how should i read and display in following program ? i am able to call the jar successfully but how to display the output ?

import java.io.InputStream;
public class call_xml_jar {
public static void main(String argv[]) {

      try{

    // Run a java app in a separate system process
     Process proc = Runtime.getRuntime().exec("java -jar xml_validator.jar");

    // Then retreive the process output
     InputStream in = proc.getInputStream();
     InputStream err = proc.getErrorStream();
     System.out.println("Completed...");

      }
      catch (Exception e) {
            e.printStackTrace();
      }
 }
}

Output: Completed... I want to print jar output as well

logan
  • 6,978
  • 34
  • 98
  • 171

2 Answers2

2

With the lines

InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();

you are already on the way.

These streams give you access to the other application's standard output and standard error streams (respectively). By the way: You retrieve the other application's standard output stream by calling getInputStream(), as this is the view of your current application; you are inputting the other application's data.

Just to make it clear: The standard output and th standard error stream are accessed in an application by printing calls to System.out and System.err (respectively).

So, if you have - for example - System.out.println("Hello world") in the other application, you will retrieve the corresponding bytes (see below) in the input stream that you reference with the variable in of the above code snippet.

Normally, you are not interested in the bytes but you want to retrieve the String that you have placed into the output. So you must convert the bytes to a String. For this you normally must provide an encoding (the Java class for that is Charset). In fact, the platform's default encoding works in such cases.

The easiest way is to wrap the input stream in a buffered reader:

BufferedReader outReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

The above mntioned platform's default encoding is used, when not specifying any character set in the InputStreamReader's constructor.

A BufferedReader knows a method readLine(), which you must use to get all the other application's output.

while(outReader.ready())
    System.out.println(outReader.readLine())

One word about flushing: If you write data to the standard output stream, this stream is flushed only, when a newline is also written. This is done by calls to System.out.println(...). And this is the reason, why you must read entire lines from the reader.

Are you now able to assemble some code that reads out the other application's output? If not, you maybe should post another question.

Seelenvirtuose
  • 19,157
  • 6
  • 34
  • 57
0

I solved it myself.. Here is my solution...

// Then retreive the process output
 InputStream in = proc.getInputStream();
 InputStream err = proc.getErrorStream();
 System.out.println("Completed...");

 InputStreamReader is = new InputStreamReader(in);
 StringBuilder sb=new StringBuilder();
 BufferedReader br = new BufferedReader(is);
 String read = br.readLine();
 while(read != null) {
        //System.out.println(read);
        sb.append(read);
        sb.append("\n");
        read =br.readLine();
    }
 System.out.println(sb);
logan
  • 6,978
  • 34
  • 98
  • 171