Scripting in Java (javax.script)

Java 6.0 comes with a new package called javax.script that enables Java programs to interact with various scripting languages. The javax.script specification (JSR 223) provides a common interface to access any scripting implementation.

The ability to interact with a scripting language from Java has a few advantages. They are

The BeanShell scripting language is different from the rest of the pack because it supports much of the Java syntax (it has not been enhanced to support the JDK 5.0 and above features yet). The advantage of this is, when doing the rapid prototyping, one could use BeanShell to write the logic and later convert it into a compiled java class.

BeanShell currently does not have the javax.script specifiation implemented. As a result, it's not possible to use it using the standard javax.script interface. While this was acceptable during the JDK 5.0 days when javax.script package was available but not part of the standard java, it is very desirable now that JDK 6.0 has javax.script included in it.

While I have used the JDK 6.0's JavaScript scripting engine, there are times I would prefer using BeanShell as well (Ofcourse, nothing against JavaScript). So, I have tried to implement the javax.script speficiation for BeanShell. You can download the BeanShell javax.script implementation jar and the source code. It is licensed under LGPL. You also need to have the BeanShell library.

Usage: All you need to do is


    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("BeanShell");
    // use the javax.script.ScriptEngine interface 

© 2008 Dirisala.Net/scripting