Question

I've been trying to use Apache Sling's Scala 2.8 Script Engine recently updated last month. I came from using Scala 2.7 along with Sling's Scala 2.7 Script Engine and that worked great. I run into a problem when I try to use the new implementation. When calling ScalaScriptEngine's eval function I always receive an "Error executing script" due to a NullPointerException. Has anyone else worked with the new build of the script engine and run into this as well?

Thanks! Steven

Was it helpful?

Solution

There is a bug which prevent the Scala scripting engine from being used standalone. See https://issues.apache.org/jira/browse/SLING-1877 for details and a patch.

Also note, that with the patch applied you still need to set the class path when using the scripting engine. This is a change from 2.7.7 where the default java class path (i.e. java.class.path) was used automatically. In 2.8 you have to set this explicitly thorough the '-usejavacp' argument.

Here is some sample code demonstrating the standalone usage of the Scala scripting engine:

def testScalaScriptEngine() {
  val scriptEngineFactory = new ScalaScriptEngineFactory

  val settings = new ScalaSettings()
  settings.parse("-usejavacp")
  scriptEngineFactory.getSettingsProvider.setScalaSettings(settings)

  val scriptEngine = scriptEngineFactory.getScriptEngine

  val script = """
  package script {
    class Demo(args: DemoArgs) {
      println("Hello")
    }
  }
  """
  scriptEngine.getContext.setAttribute("scala.script.class", "script.Demo", ScriptContext.ENGINE_SCOPE)
  scriptEngine.eval(script)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top