Pure Danger Tech


navigation
home

Making BSF and Rhino work

03 Jan 2007

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:

  1. Download the latest Rhino engine from Mozilla and grab lib/bsf.jar.
  2. Download the latest BSF binary from Apache Jakarta and grab js.jar
  3. Download the Commons Logging Framework from Apache Jakarta and grab commons-logging-1.1.jar.
  4. Compile this Java test class which will execute a javascript file passed as an argument: <pre>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);
}   }

</pre>

  1. Define a simple Javascript file test.js like this: <pre>var x = 5 * 10 java.lang.System.out.println(“x: “ + x)

</pre>

  1. Execute your TestBsh class using the jars on the classpath and pass path to test.js as an argument:
`I wanted to try out using the [Rhino](http://www.mozilla.org/rhino/doc.html) javascript engine with the [Bean Scripting Framework](http://jakarta.apache.org/bsf/) and had a hard time finding many clues about how to put these pieces together and make something work. So here&#8217;s my report in case it helps someone else.

Basic steps:

  1. Download the latest Rhino engine from Mozilla and grab lib/bsf.jar.
  2. Download the latest BSF binary from Apache Jakarta and grab js.jar
  3. Download the Commons Logging Framework from Apache Jakarta and grab commons-logging-1.1.jar.
  4. Compile this Java test class which will execute a javascript file passed as an argument: <pre>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);
}   }

</pre>

  1. Define a simple Javascript file test.js like this: <pre>var x = 5 * 10 java.lang.System.out.println(“x: “ + x)

</pre>

  1. Execute your TestBsh class using the jars on the classpath and pass path to test.js as an argument:

`

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.