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("LoadConfig 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?
jksungAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Can you post some example output of that Tcl command that you are trying to capture? Also, I don't have a lot of experience with Tcl, what does LoadConfig do?

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.
0
jksungAuthor Commented:
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.executeHCMDCommandString + 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
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Hmm, yeah, it is a bit hard to debug from my end as it is one of those questions where it depends on a few non-standard/third-party things. Also, I am not able to access the source code for JavaTCL from here at work, which might have been useful.

So at this point, I can only suggest two more things...

1. Try redirecting the stderr as well, ie. System.setErr(new PrintStream(out));   as it may be possible that the output that you are after is being sent to err rather than out

2. Try redirecting the streams (either or both) *before* you instantiate the Interp object. Maybe, the code in Interp's constructor is saving the value of those streams and hence if you instantiate it before you redirect, then the redirect has no effect on it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jksungAuthor Commented:
Thanks, the problem was I have to redirect the stream before instantiating the Interp object.  Thanks so much for your help!
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not a problem, I'm glad that I was able to assist, especially since it was a pretty obscure issue!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.