Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Java Q&A


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:

  • As a batch process executing a myprogram.tcl.
  • Interactively, prompting and responding to users for each line.

  • Interpretively, with users using source myprogram.tcl to load the whole program into the interpreter, while keeping an interactive prompt for debugging and experiments.

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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.