Re: [tcljava-user] help - how to execute a tcl from java
Brought to you by:
mdejong
|
From: Mo D. <mo...@mo...> - 2007-10-01 21:57:37
|
rohit wrote:
> good morning,
>
> am trying to execute a simple tcl script from a java stub. below is
> the java stub and the tcl script.
>
> i am able to execute the script. however, i need to get the results of
> the tcl script execution into a output stream / buffer in the calling
> java stub. i am not able to figure how this is done. please help !
>
> any pointers/suggestions will be highly appreciated
>
> java stub :
> Interp interp = new Interp();
>
> try {
> interp.evalFile("test.tcl");
> System.out.println("running tcl done.");
> }
> catch (TclException ex) {
> // some exception handling
> }
> }
> finally {
> interp.dispose();
> }
>
> test.tcl contents :
> set a 3
> set b 4
> set message "c = sqrt($a*$a+$b*$b) = [expr sqrt($a*$a+$b*$b)]"
> puts $message
>
Hello Rohit
Folks keep asking for this sort of functionality in Jacl, so I went
ahead and created a patch to make this
much easier. What I did was make the tcl.lang.StdChannel class public
and add the methods setIn,
setOut, and setErr. These work like the same methods in the Java System
class except that the streams
will store only data written to stdout or stderr from Tcl.
I also updated the extras/GuiShell/GuiShell.java example so that it
works with swing under JDK 1.4 and
makes use of the new methods in the StdChannel class to implement the
kind of redirection you want to do.
I think a working example shows exactly what is needed. You can apply
this patch to the CVS or just grab
the CVS tree fresh.
Another approach you could use, which is a lot more simple would be to
overload the puts command and
then save the results in a variable. Your script could then set the
interp result at the end to be the value
of this variable. you could also just set the interp result directly,
instead of worrying about using puts, but
it depends on what you are wanting to do.
I hope that thelps
Mo DeJong
|