« Putting extra state on enum constants | Home | SuppressWarnings »
Making BSF and Rhino work
I wanted to try out using the Rhino javascript engine with the Bean Scripting Framework and had a hard time finding many clues about how to put these pieces together and make something work. So here’s my report in case it helps someone else.
Basic steps:
- Download the latest Rhino engine from Mozilla and grab lib/bsf.jar.
- Download the latest BSF binary from Apache Jakarta and grab js.jar
- Download the Commons Logging Framework from Apache Jakarta and grab commons-logging-1.1.jar.
- Compile this Java test class which will execute a javascript file passed as an argument:
import java.io.FileReader; import java.io.IOException; import org.apache.bsf.BSFException; import org.apache.bsf.BSFManager; import org.apache.bsf.util.IOUtils; public class TestBSF { public static void main(String[] args) throws BSFException, IOException { String fileName = args[0]; // Determine the language from the script file extension String language = BSFManager.getLangFromFilename (fileName); // Read the contents of the script file into a String FileReader in = new FileReader (fileName); String script = IOUtils.getStringFromReader (in); // Construct the manager which manages the scripting engines BSFManager mgr = new BSFManager(); // Execute with the proper language, the fileName (for error reporting), // the row and column to start at, and finally the contents of the script mgr.exec (language, fileName, -1, -1, script); } } - Define a simple Javascript file test.js like this:
var x = 5 * 10 java.lang.System.out.println("x: " + x) - Execute your TestBsh class using the jars on the classpath and pass path to test.js as an argument:
java -cp .;bsh.jar;js.jar;commons-logging-1.1.jar TestBSF test.js
x: 50
This script accesses the Java System object from the Javascript script. You can also go the other way by registering objects (beans) with the manager before executing the script. Add this code in the Java portion to declare an object available to the script. The first argument is the name of the bean, the second is the bean object, and the third is the bean type.
mgr.declareBean("myString", "spackle", String.class);
Then access that value in the Javascript with something like this:
var s = bsf.lookupBean("myString")
java.lang.System.out.println(s)
This is just the tip of iceberg, of course, but hopefully this is a big headstart. In the newly released Java 6, Rhino is bundled with the JDK and integrated through JSR 223, which effectively takes the place of BSF. You can find some examples and more info here. I wrote this article with BSF as Java 6 is not an option for me and probably isn’t for most people yet but that is definitely the right choice if it’s an option.
About this entry
You’re currently reading “Making BSF and Rhino work,” an entry on Pure Danger Tech
- Published:
- Jan 03 2007 / 10:29 pm
- Category:
- programming, Java, scripting, javascript
- Vote:
- Other posts with these tags:

1 Comment
Jump to comment form | comments rss | trackback uri