开发者

Using Apache Sling's Scala 2.8 Script Engine

开发者 https://www.devze.com 2023-01-25 05:25 出处:网络
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

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 开发者_JS百科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


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)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消