Thread: [tcljava-user] HELP...Calling TCL scripts from a Java program
Brought to you by:
mdejong
From: Tam H. <th...@su...> - 2001-05-11 00:41:25
|
I'm trying to execute a Tcl script from a java program using the java Runtime class. However, the script does not execute because my log file is never created. Should i stick to this Runtime class or use something like Tcl blend to include a class to call the script. I'm just trying to find the quickest solution to call the script, pass it parameters, and get results from the script in return..Am i even calling the script the right way? Any suggestions would be appreciated!! TCL SCRIPT: set f "log.txt" set out [open $f w] puts $out $argv0 puts $out $argv1 puts $out $argv2 close $out JAVA PROGRAM: public static void main(String args[]) { try { String s; String[] argv = {"hey", "there", "buddy"}; String[] cmd = new String[2]; cmd[0] = "d:\\Tcl83\\bin\\wish83.exe"; cmd[1] = "d:\\cygwin\\bin\\hello.tcl"; Process myProcess = Runtime.getRuntime().exec(cmd, argv); myProcess.waitFor(); System.out.println("Process exit code is: " + myProcess.exitValue()); DataInputStream in = new DataInputStream(myProcess.getInputStream()); while ((s = in.readLine()) != null) { System.out.println(s); } } catch (Throwable t) { System.err.println(t); t.printStackTrace(); //Ensure the application exits with an error condition. System.exit(1); } } |
From: Tam H. <th...@su...> - 2001-05-12 20:23:19
|
ok, i'm trying the other approach by using the binary version of Jacl 1.2.6. i'm trying to call the tcl script as describe below..am i missing any kind of patch for jacl? both tcljava.jar and jacl.jar are on my classpath and i'm using Forte for Java 1.0. i know the interp object is created bc it's fine when i type newInterp.eval("puts {hello there}"), but doesn't invoke my script when i use "source".. ERRORS: tcl.lang.TclVarException tcl.lang.TclVarException at tcl.lang.Var.lookupVar(Var.java) at tcl.lang.Var.getVar(Var.java) at tcl.lang.Parser.evalTokens(Parser.java) at tcl.lang.Parser.eval2(Parser.java) at tcl.lang.Interp.eval(Interp.java) at tcl.lang.Interp.evalFile(Interp.java) at tcl.lang.SourceCmd.cmdProc(SourceCmd.java) at tcl.lang.AutoloadStub.cmdProc(Extension.java) at tcl.lang.Parser.evalObjv(Parser.java) at tcl.lang.Parser.eval2(Parser.java) at tcl.lang.Interp.eval(Interp.java) at tcl.lang.Interp.eval(Interp.java) at com.nwoods.jgo.examples.demo1.Demo1.main(Demo1.java:1006) JAVA PROGRAM: import tcl.lang.*; public static void main(String args[]) { try { Interp newInterp = new Interp(); newInterp.eval("source d:/Tcl83/bin/hello.tcl"); newInterp.dispose(); } catch (Throwable t) { System.err.println(t); t.printStackTrace(); //Ensure the application exits with an error condition. System.exit(1); } } |
From: Mo D. <md...@cy...> - 2001-05-12 20:35:04
|
On Sat, 12 May 2001, Tam Huynh wrote: > ok, i'm trying the other approach by using the binary version of Jacl 1.2.6. > i'm trying to call the tcl script as describe below..am i missing any kind > of patch for jacl? both tcljava.jar and jacl.jar are on my classpath and > i'm using Forte for Java 1.0. i know the interp object is created bc it's > fine when i type newInterp.eval("puts {hello there}"), but doesn't invoke my > script when i use "source".. > > ERRORS: > > tcl.lang.TclVarException > tcl.lang.TclVarException > at tcl.lang.Var.lookupVar(Var.java) > at tcl.lang.Var.getVar(Var.java) > at tcl.lang.Parser.evalTokens(Parser.java) > at tcl.lang.Parser.eval2(Parser.java) > at tcl.lang.Interp.eval(Interp.java) > at tcl.lang.Interp.evalFile(Interp.java) > at tcl.lang.SourceCmd.cmdProc(SourceCmd.java) > at tcl.lang.AutoloadStub.cmdProc(Extension.java) > at tcl.lang.Parser.evalObjv(Parser.java) > at tcl.lang.Parser.eval2(Parser.java) > at tcl.lang.Interp.eval(Interp.java) > at tcl.lang.Interp.eval(Interp.java) > at com.nwoods.jgo.examples.demo1.Demo1.main(Demo1.java:1006) > > > JAVA PROGRAM: > import tcl.lang.*; > > > > public static void main(String args[]) > { > try > { > Interp newInterp = new Interp(); > newInterp.eval("source d:/Tcl83/bin/hello.tcl"); > newInterp.dispose(); > } > catch (Throwable t) > { > System.err.println(t); > t.printStackTrace(); > //Ensure the application exits with an error condition. > System.exit(1); > } > } Well, I don't know why running the source command would do that. It is running Interp.evalFile() which should eval the contents of the file. Could you poke around in a debugger to find out why this variable error is being generated? Is your script using a variable that is not set? Mo |
From: Mo D. <md...@cy...> - 2001-05-11 00:48:34
|
On Thu, 10 May 2001, Tam Huynh wrote: > I'm trying to execute a Tcl script from a java program using the java > Runtime class. However, the script does not execute because my log file is > never created. Should i stick to this Runtime class or use something like > Tcl blend to include a class to call the script. I'm just trying to find > the quickest solution to call the script, pass it parameters, and get > results from the script in return..Am i even calling the script the right > way? Running tclsh from a Java application should not be that hard. Trouble is, the Java implementation from Sun is really full of bugs under Windows. You could take a look at the Jacl implementation of the exec command for Tcl. I have to warn you, it is a bit scary! > Any suggestions would be appreciated!! Have you considered using Jacl? Jacl is a Tcl interp written in 100% Java. Adding Jacl to your existing Java application might be easier, you just add tcljava.jar and jacl.jar to the CLASSPATH and then create a tcl.lang.Interp object. Mo DeJong Red Hat Inc |
From: D. J. H. <dha...@mi...> - 2001-05-12 03:08:47
|
The fundamental problem here is that Runtime.exec(...) is a very primitive interface and hard to use correctly, especially to capture large chunks of output from stdout/stderr or input large chunks of data to stdin. The following article on JavaWorld provides a good example at the end: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html I don't know if this "fix" has yet been applied to Jacl's exec, per Mo's reply, but I was intending to do so someday if someone hasn't already... In addition, you should really use a BufferedReader(InputStreamReader()) chain around the exec'd process output if you want to capture text lines. DataInputStream is for binary struct-type and serialized Java object data, not text. -=- D. J. Tam Huynh wrote: > I'm trying to execute a Tcl script from a java program using the java > Runtime class. However, the script does not execute because my log file is > never created. Should i stick to this Runtime class or use something like > Tcl blend to include a class to call the script. I'm just trying to find > the quickest solution to call the script, pass it parameters, and get > results from the script in return..Am i even calling the script the right > way? [ . . . ] > Process myProcess = Runtime.getRuntime().exec(cmd, argv); > myProcess.waitFor(); > System.out.println("Process exit code is: " + > myProcess.exitValue()); > > DataInputStream in = new > DataInputStream(myProcess.getInputStream()); [ . . . ] |