hide random home http://www.javasoft.com/applets/alpha/notapplets/tcl/index.html (PC Press Internet CD, 03/1996)

Java/Tcl interface

Java/Tcl interface

This page contains a reference to a prototype of a Java/Tcl interface. It allows you to execute simple Tcl scripts from Java, and implement Tcl commands as Java primitives.

The code was originally written by Patrick Naughton in November of 1994. I have made only minor changes since then.

Check out the main TclInterpreter class. To execute a Tcl script you have to create an instance of the TclInterpreter class and call the eval(String) method on it.

	TclInterpreter tcl = new TclInterpreter();
	tcl.eval("set a 33");
	tcl.eval("puts [$a + 1]");
To define a new Tcl primitive you have to subclass the TclInterpreter, define a method that takes an array of strings as an argument and returns a string and bind the method.
	class MyTclInterpreter extends TclInterpreter {
	    MyTclInterpreter() {
		bind("pair");
	    }

	    String pair(String argv[]) {
		return "(" + argv[0] + "," + argv[1] + ")";
	    }
	}
A slightly more elaborate example can be found in TclTest.java.

How to get it.

You can ftp the sources (not including the Tcl sources) with the binaries precompiled for Solaris from ftp://java.sun.com/pub/java/avh/java.tcl.tar.Z (297 Kb). Check out the README on how to run the example.

This is totally unsupported sample code. It is for your entertainment only.


avh@eng.sun.com