Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Script JavaBeans with the Bean Scripting Framework

Add scripts to your JavaBeans or JavaBeans to your scripts

  • Print
  • Feedback
The Bean Scripting Framework (BSF) is one of the more interesting Java offerings available free at IBM's alphaWorks site (see Resources). BSF lets Java programs run scripts written in other languages and also allows other scripting languages to use existing Java classes. If you have useful scripts written in Python or Perl, for example, or have some useful BasicScript program you don't want to reimplement in Java, BSF will let you call that script from your Java program. BSF can also do the reverse, letting you use Java facilities and existing classes from your scripting language. BSF currently supports interoperability between Java and the various languages that appear in Table 1. The documentation shipped with BSF indicates that support for more languages is in development. All of the languages listed are freely available, though the restrictions for commercial use vary. Downloading information for each language is available from the documentation shipped with BSF.

Some languages (as indicated in the table below), run only on Windows 95 or later, because the ActiveX scripting technology on which they rely is limited to Windows. The rest of the languages run portably on any system that has a Java Virtual Machine (JVM), since they are written in portable Java.

Language Description
BML IBM's Bean Markup Language
JScript Microsoft's implementation of JavaScript. Windows only.
Mozilla Rhino Netscape JavaScript, available from Mozilla. Written in Java.
NetRexx IBM's Java implementation of the Rexx editor language. Written in Java.
PerlScript ActiveState's Perl scripting environment. Windows only.
VBScript VisualBasic scripting environment. Windows only.
Jacl Java implementation of a large subset of Tcl.


JPython Java implementation of Python.
LotusXSL IBM/Lotus implementation of the XSL language
Scripting languages supported by BSF


In this article, I'll explain why you may want to use BSF in your application, whether you're writing it in a scripting language or in Java. You'll see how to use an existing Java class from a script written in a scripting language. Then, you'll see a Java program that can evaluate scripts written in other languages and can use the results. Finally, I'll explain some of the ways IBM is using BSF in its own products.

Why scripting?

I've always found Java's lack of an eval statement to be a bit disappointing. Of course, I understand the reasons for leaving it out. The JVM is big enough without building a Java compiler into it, and eval would add another level of complexity to security control. Besides, a lot of the things I'd like to do with eval can be done more efficiently, elegantly, and type-safely with class loading. But still, sometimes I just want to be able to evaluate an expression. Is that too much to ask?

Since I was born and raised on Unix, my head is filled with arcana about various scripting languages I've learned. I've written a lot of useful scripts over the years, some of them quite complex. (I once wrote a compiler-compiler in Perl. Don't ask.) I'd love to be able to use my scripts from Java, but doing so by way of System.runtime() is what I call "3-I": inefficient, inelegant, and icky.

  • Print
  • Feedback

Resources
  • Download the source code and class files for this month's article:
  • Note: These languages are applicable to Windows 95, Windows 98, and Windows NT 4.0 only. Also note that to use these scripting languages with Windows 95, you'll need Windows 95 OSR2, or Internet Explorer 4.0 or later; or you'll have to install DCOM. International versions of Windows 95 also require DCOM.