You can also rebind stdout

http://ptolemy.eecs.berkeley.edu/~cxh/java/tclblend/println.html
sez

--start--
Hints about
java.lang.System.out.println and Tcl
Blend

Calling println from Tcl Blend

The Tcl Blend code below calls System.err.println 

set err [java::field System err]
$err {println String} foo

To see the signatures of the methods that can be run on $err, eval
java::info methods $err 

Rebinding stdout and stderr

Java code sometimes prints to stdout or stderr, and it would be nice
to capture this output so that Tcl
can process it. 

The Tcl Blend code below rebinds err to a stream and then gets the
String value of the stream. 

set stream [java::new java.io.ByteArrayOutputStream]
set printStream [java::new \
    {java.io.PrintStream java.io.OutputStream} $stream]

java::call System setErr $printStream

set err [java::field System err]
$err {println String} foo

$printStream flush
puts "The error was [$stream toString]"

jdkCapture

util.tcl contains the jdkCapture proc: 

# Capture output to System.out
proc jdkCapture {script varName} {
    upvar $varName output
    set stream [java::new java.io.ByteArrayOutputStream]
    set printStream [java::new \
            {java.io.PrintStream java.io.OutputStream} $stream]
    set stdout [java::field System out]
    java::call System setOut $printStream
    set result [uplevel $script]
    java::call System setOut $stdout
    $printStream flush
    set output [$stream toString]
    return $result
}

Below is an example: 

% jdkCapture {puts stderr "This output goes to stderr"} erroutput
This output goes to stderr
% set erroutput
% 
% jdkCapture {puts stdout "This output goes to stdout"} stdoutput
% set stdoutput
This output goes to stdout

% 
--end--

-Christopher
--------

    
    
    Lubos Vrba wrote:
    
    > Thanks for advice, but I just don't know how use it.
    > May be I definition of my problem was pure. I'll try to do it better:
    > In next.tcl isn't only one puts commands!
    
    i hope :-)
    
    > I don't want tcl script print some messages!
    
    ok, but i suppose you want to retrieve some value returned by your script
    next.tcl?
    
    > I dont want to use some procedures in tcl (if it's possible).
    
    I dont know how to do it without proc ...
    
    > I don't know how to use getResult().toString(), it still returns nothing!
    
    here is the sources i am using (in which getResult().toString() returns
    something):
    
    the shell in java:
    
       [...]
      // start shell interpretation
      while(!theCommand.equals("exit")) {
       System.out.print("> ");
    
        // try to readln the command typed
       try {
        StringBuffer theLineRead = new StringBuffer(
         (new BufferedReader(new InputStreamReader(System.in))).readLine()
        );
        theCommand = theLineRead.toString();
       } catch (IOException e ){
        e.printStackTrace();
       }
    
       // try to evaluate command read
       try {
        theInterpretor.eval(theCommand);
        System.out.println("returned value = '"+
    theInterpretor.getResult().toString() + "'");
       } catch (TclException e) {
        e.printStackTrace();
       }
     }
    [...]
    
    if you use a shell interpreting like that, you can type:
    > puts foo
    foo
    returned value = ''
    > return foo
    tcl.lang.TclException
            at tcl.lang.ReturnCmd.cmdProc(ReturnCmd.java:99)
            at tcl.lang.Parser.evalObjv(Parser.java:740)
            at tcl.lang.Parser.eval2(Parser.java:1138)
            at tcl.lang.Parser.evalTokens(Parser.java:930)
            at tcl.lang.Parser.eval2(Parser.java:1125)
            at tcl.lang.Interp.eval(Interp.java:1781)
            at tcl.lang.Interp.eval(Interp.java:1810)
            at com.odisei.application.tcl.ShellTcl.<init>(ShellTcl.java:87)
            at com.odisei.application.tcl.ShellTcl.main(ShellTcl.java:34)
    > proc bar {} {return foo}
    
    returned value = ''
    > bar
    foo
    returned value = 'foo'
    >
    
    I don't know if it helps you, but i means :
    - puts do not set the result of the interpretor, it simply prints something
    on
    stdout (then its not interesting)
    - retrurn can not be used outside a proc
    - if a proc return something, the returned value can be retrieved using
    interp.getResult() (then you have to use a proc!)
    
    conclusion, if you want to get a value evaluated by a script, you should wr
   ite
    next.tcl like that:
    ----------------------------
    proc myProc {parameters} {
        evalutate whateverYouWant
        return theEvaluatedValue
    }
    myProc {"foo" "bar"}
    ----------------------------
    write in your java shell implementation
    
    theInterpretor.eval("source next.tcl");
    System.out.println("returned value = '"+ theInterpretor.getResult().toStrin
   g()
    + "'");
    
    and you will get the value evaluated by your script. i hope this is a solut
   ion
    to your problem.
    
    philippe - france
    
    
    
    
    ----------------------------------------------------------------
    The TclJava mailing list is sponsored by WebNet Technologies.
    To subscribe:    send mail to [EMAIL PROTECTED]  
                     with the word SUBSCRIBE as the subject.
    To unsubscribe:  send mail to [EMAIL PROTECTED] 
                     with the word UNSUBSCRIBE as the subject.
    To send to the list, send email to '[EMAIL PROTECTED]'. 
    A list archive is at: http://www.findmail.com/listsaver/tcldallas/
--------

----------------------------------------------------------------
The TclJava mailing list is sponsored by WebNet Technologies.
To subscribe:    send mail to [EMAIL PROTECTED]  
                 with the word SUBSCRIBE as the subject.
To unsubscribe:  send mail to [EMAIL PROTECTED] 
                 with the word UNSUBSCRIBE as the subject.
To send to the list, send email to '[EMAIL PROTECTED]'. 
A list archive is at: http://www.findmail.com/listsaver/tcldallas/

Reply via email to