jksung
asked on
Capturing standard output from JavaTcl Interp
I am running third party TCL functions with Java using the JavaTCL library. I want to capture what is being sent to standard out for commands such as:
import tcl.lang.Interp;
import tcl.lang.TclException;
public class Temp {
public Interp tclInterp;
public static void main() {
tclInterp = new Interp;
tclInterp.eval("LoadConfig xxx.cfg");
}
}
I've tried redirecting the standard out to something like a ByteArrayOutputStream, however, that only captures what is specifically send to System.out from the program, it does not capture what is being sent to standard out from the command "tclInterp.eval("LoadConfi g xxx.cfg");".
For example, if I do something like:
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
tclInterp.eval("LoadConfig xxx.cfg");
System.out.println("temp") ;
String collected = new String(out.toByteArray());
the string "collected" will only contain "temp".
Anyone have any ideas?
import tcl.lang.Interp;
import tcl.lang.TclException;
public class Temp {
public Interp tclInterp;
public static void main() {
tclInterp = new Interp;
tclInterp.eval("LoadConfig
}
}
I've tried redirecting the standard out to something like a ByteArrayOutputStream, however, that only captures what is specifically send to System.out from the program, it does not capture what is being sent to standard out from the command "tclInterp.eval("LoadConfi
For example, if I do something like:
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
tclInterp.eval("LoadConfig
System.out.println("temp")
String collected = new String(out.toByteArray());
the string "collected" will only contain "temp".
Anyone have any ideas?
ASKER
LoadConfig is just part of a third party TCL API provided to us, it could be any TCL function.
Some sample output to standard out would be:
Attempting to load configuration xxx on host Server1.
Loading IP Configuration...
Configuration has been exported.
If I run code such as:
PrintStream orgStream = System.out;
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
tclInterp.eval(VarUtils.ex ecuteHCMDC ommandStri ng + cmd);
System.out.println("temp") ;
String collected = new String(out.toByteArray());
System.setOut(orgStream);
System.out.println("temp2" );
Output would be:
Attempting to load configuration xxx on host Server1.
Loading IP Configuration...
Configuration has been exported.
temp2
And the contents of the string "collected" would only be:
temp
Some sample output to standard out would be:
Attempting to load configuration xxx on host Server1.
Loading IP Configuration...
Configuration has been exported.
If I run code such as:
PrintStream orgStream = System.out;
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
tclInterp.eval(VarUtils.ex
System.out.println("temp")
String collected = new String(out.toByteArray());
System.setOut(orgStream);
System.out.println("temp2"
Output would be:
Attempting to load configuration xxx on host Server1.
Loading IP Configuration...
Configuration has been exported.
temp2
And the contents of the string "collected" would only be:
temp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, the problem was I have to redirect the stream before instantiating the Interp object. Thanks so much for your help!
Not a problem, I'm glad that I was able to assist, especially since it was a pretty obscure issue!
I ask because what you have done *should* capture what you want. The above info may help me to work out how JavaTcl is outputting data so that I can help you to capture it.