Java Q&A

Can you script Java with Tcl? Sure, and TclBlend is a great way to do it.


March 01, 2001
URL:http://www.drdobbs.com/jvm/java-qa/184404539

Mar01: Java Q&A

When Should You Script Java With Tcl?

Cameron is principle of Phaseit, a consulting firm. He can be contacted at [email protected].


Can you script Java development with Tcl? Absolutely. In fact, the tag team of Tcl and Java can serve as a scripting engine or extension language for finished applications, execute within Java Server Pages (JSPs), and be used to prototype GUIs. I frequently use Tcl for its GUI capabilities and aptitude for scripting test suites — both of which are, in part, consequences of Tcl's ability to bind simultaneously to C and Java.

The Tcl/Java combination is packaged in two different forms — Jacl and TclBlend. Jacl is an implementation of Tcl in Java. However, TclBlend, the implementation that I'll focus on here, is a different kind of system. TclBlend's architecture is that of a conventional Tcl language processor, extended with awareness of a Java Virtual Machine (JVM). In the first place, that means you can do with TclBlend all the things people typically do with Tcl — write tiny (but effective) administrative scripts, prototype GUIs, load customized extensions, control clumsy TTY-oriented applications through Expect, and the like. At the same time, you can access any available Java classes and even introspect on their interfaces through Java's reflection capability. (For more information on TclBlend, see "TclBlend: Blending Tcl and Java," by Scott Stanton, DDJ, February 1998.) TclBlend 1.2.6 is freely available at http://dev.scriptics.com/software/java/download.html.

Suppose your team has developed a new piece of hardware — an air purifier that emits negative ions into the room. You provide a minimal Java interface for it. Creation of an instance of the Java class AirPurifier turns the device on and has it begin its automatic operation. You can also invoke the method AirPurifier->standard() to release a standardized charge quantity in a single 20-millisecond pulse.

How do you exercise the device? It's easy enough to write a couple of 10-line control programs in Java, compile them, and invoke them from the command line when you want to run the AirPurifier methods. It would only be another dozen lines or so to build a small control panel using the Abstract Windowing Toolkit (AWT) or Swing APIs.

With about an hour's experience with TclBlend, however, you can instead write something like Example 1. Not only is this briefer than the AWT equivalent, it has several other advantages as well. Tcl's Tk GUI toolkit is more lightweight than AWT, easier to learn, has considerably simpler geometric management, and an event (or action) model that's safe in the hands of novices.

Additionally, there are benefits to scripting other than brevity. For instance, TclBlend can process Example 1 in any of three modes:

This flexibility is a great benefit during development. If all you know is the edit-compile-test cycle, or use of a conventional IDE, you may have missed the advantages of the lightweight interaction of the Tcl interpreter. Give a modern interpreter a chance sometime; experiment with its easy movement between the different modes. Especially notice the powerful introspection TclBlend provides. With Example 2, you can interactively query the interpreter for the contents of a .jar file, the handle of a keyboard action, or the (public) signature of a Java method, respectively. Moreover, because you have the full interpreter available, it's handy to program your own customizations. For example, it takes only a few lines to build in a specialized error-handler that prompts you in case you misspell a command name.

Of course, a good IDE does all of these things out of the box, generally through a point-and-click interface. However, the Tcl interpreters and consoles are far more responsive, as well as being fully programmable. Programmers working on large Java-based projects often find that even small cosmetic changes can take minutes to recompile with popular Java IDEs. Tcl consoles respond essentially instantly.

One of Tcl's strengths is its ability to automate testing operations. The largest Tcl applications are widely believed to be suites of regression tests in use by such database vendors as Sybase and Oracle. One reason for this is Tcl's excellence at "gluing," that is, at being able to invoke other processes or object modules in a consistent, reliable way. This combines with Tcl's simple syntax to make the language a natural in quality assurance departments.

Java is one of the beneficiaries of this strength. Open source Java projects such as Jikes, Jacks, and GCJ all include Tcl/Java-based regression suites. All new check-ins must include tests that confirm functionality. RedHat's Mo DeJong is the project leader for Tcl/Java, along with several other Java efforts. He first imposed the rule that "every patch needs to have a test case." Despite initial resistance, this has worked out well enough that he says they should no longer be called "regression tests." The concurrent versions system (CVS) images of the projects always pass all their own tests, so there's no regression — stuff just works.

Example 3 is a typical test taken from the Tcl/Java distribution. More specifically, this is test 10.4 of the Java Interp function. It confirms that a roundtrip from Tcl to Java and back to Tcl preserves the display value, baz, of the associative array element foo(bar).

A final benefit of TclBlend is that it tames the Java Native Interface (JNI). If you have experience with JNI, you know that it is generally regarded as a last resort. Tcl, on the other hand, lets you glue together pieces written in other languages. Therefore, one way to look at TclBlend is as a convenient shell for getting C and Java application programming interfaces (APIs) to play together nicely.

Suppose, then, that you have a large Java project that needs to access a few old C-coded drivers that aren't worth translating into Java. You need to quickly develop a small GUI that lets nontechnical workers exercise the functionality and verify its correct operation. TclBlend is perfect. You might be able to write the entire application in one interpreter session: Start the session, load in the Java classes, load the C object modules, define test patterns, and build a control panel with a few buttons and entries.

Scripting is a productive way to experiment and construct with Java classes. While several good scripting systems exist, TclBlend is unique for its accessible GUI system and convenient interface to C/C++ objects. TclBlend is particularly apt for experimental programming, test automation, and rapid development of lightweight GUIs. For more information on TclBlend resources, refer to a web page I maintain at http://starbase.neosoft.com/~claird/comp.lang.java/tclblend.html.

DDJ

Mar01: Java Q&A


package require java
java::import AirPurifier
pack [button .b -text "Start purifier" -command action]
pack [label .l]
proc action {} {
    switch [.b cget -text] {
        "Start purifier" {
           set ::purifier [java::new AirPurifier]
           .b configure -text "Emit pulse"
           set label "Purifier started."
        }
        "Emit pulse" {
           ::purifier standard
           set label "Pulse emitted."
        }
        default {
           set label ""
        }
    }
    .l configure -text $label
}

Example 1: TclBlend example.

Mar01: Java Q&A


(a)
jar $jar_filename

(b)
java::field $my_dialogue okButton

(c)
java::info fields $some_object

Example 2: (a) Interactively querying the interpreter for the contents of a .jar file; (b) the handle of a keyboard action; (c) the (public) signature of a Java method.

Mar01: Java Q&A


test javaInterp-10.4 {Java_tcl_lang_Interp_getVar} {
   catch {unset foo}
   set foo(bar) baz
   set i [java::getinterp]
   [$i {getVar java.lang.String int} foo(bar) 0] toString
} {baz}

Example 3: A typical TclBlend test.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.