Sunday, January 17, 2010

Jython ScriptEngine with WebStart

An embedded script engine is a great way to empower an application's users with the ability to customize and extend tools, and java's JSR223 ScriptEngine framework makes it easy to implement.

I recently embedded a Jython ScriptEngine in a littleware based application that runs in a WebStart environment. Problems always arise when integrating two sophisticated software frameworks like jython and webstart, but the jython ScriptEngine ran with WebStart after addressing two small problems.

First, the application links against the "standalone" jython.jar library which bundles the complete jython script engine. Second, the application includes the following jython-specific code to add jython.jar to the python library search path.

...
final PySystemState engineSys = new PySystemState();
engineSys.path.append( Py.newString( "__pyclasspath__/Lib" ) );
Py.setSystemState(engineSys);
final ScriptEngine jython = new ScriptEngineManager().getEngineByName("jython");
if (null == jython) {
   throw new IllegalStateException("Failed to load jython ScriptEngine");
}
...

No comments: