How do I set up jsr223 scripting with scala as scripting language
Asked Answered
V

3

7

So far I have tried the sling implementation for jsr223 scripting for scala, but was not able to get it set up correctly. when I do this:

public static void main(String[] args) {
    try {
        new ScriptEngineManager().getEngineByName("scala").
          eval("object HelloWorld {def main(args: Array[String]) { 
                  println(\"Hello, world!\") }}");
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

I got nothing but:

javax.script.ScriptException: ERROR 
org.apache.sling.scripting.scala.Script line 13 : not found: type 
Script at org.apache.sling.scripting.scala.ScalaScriptEngine.eval(ScalaScriptEngine.scala:117)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)

similar Problems are discussed here: http://scala-programming-language.1934581.n4.nabble.com/How-to-compile-Scala-code-from-java-using-the-current-ClassLoader-instead-of-a-string-based-classpat-td1955873.html#a1955873

and

http://dev.day.com/discussion-groups/content/lists/sling-dev/2009-12/2009-12-01_Scala_scripting_support_was_Re_And_another_one____Michael_D_rig.html

maybe there is another Implementation that I'm not aware of.

Any help appreciated

Vibraculum answered 13/4, 2011 at 19:58 Comment(0)
H
6

Have a look at the test cases in the scala/script module of Apache Sling for a working example. The script and its entry point (that is the object) need to follow certain conventions. I'll provide more information on these if required later.

For a general overview of the scripting engine see my session slides from Scala Days 2010.

Update: Scripts must be of the following form:

package my.cool.script {
  class foo(args: fooArgs) {
    import args._ // import the bindings
    println("bar:" + bar)
  }
}

The type of args is generated by the script engine and is named after the simple class name of the script appended with 'Args'. Further the example assumes, that the Bindings passed for script evaluation contains a value for the name 'bar'. For further details see the class comment on ScalaScriptEngine.

You need to pass the name of your script class to the script engine. You do this by putting the fully qualified script name (i.e. my.cool.script.foo) into the ScriptContext by the name 'scala.script.class'.

Hasty answered 13/4, 2011 at 22:34 Comment(0)
A
4

With the conclusion of https://issues.scala-lang.org/browse/SI-874 in version 2.11, it should be as easy as what is shown in the ticket:

import javax.script.*;
ScriptEngine e = new ScriptEngineManager().getEngineByName("scala");
e.getContext().setAttribute("label", new Integer(4), ScriptContext.ENGINE_SCOPE);
try {
    engine.eval("println(2+label)");
} catch (ScriptException ex) {
    ex.printStackTrace();
}
Alpine answered 2/6, 2013 at 14:40 Comment(1)
With the current Scala Version 2.11.0-M4 there need to be done some extra work to configure the environment to use the java classpath: ... List nil = Nil$.MODULE$; $colon$colon vals = $colon$colon$.MODULE$.apply((String) "true", nil); ((IMain)engine).settings().usejavacp().tryToSet(vals);Vibraculum
V
0

Unfortunately my comment was unreadable without linebreaks - so...

To be able to run the Codesnippet mentioned I needed to make the following changes. I used Scala 2.11.0-M4

public static void main(String args[]){
  ScriptEngine engine = new ScriptEngineManager().getEngineByName("scala");

  // Set up Scriptenvironment to use the Java classpath
  List nil = Nil$.MODULE$;
  $colon$colon vals = $colon$colon$.MODULE$.apply((String) "true", nil);
  ((IMain)engine).settings().usejavacp().tryToSet(vals);ScriptContext.ENGINE_SCOPE);

  engine.getContext().setAttribute("labelO", new Integer(4), ScriptContext.ENGINE_SCOPE);
  try {
    engine.eval("val label = labelO.asInstanceOf[Integer]\n"+
                "println(\"ergebnis: \" + (2 + label ))");
  } catch (ScriptException ex) {
    ex.printStackTrace();
  }
}
Vibraculum answered 22/10, 2013 at 8:51 Comment(1)
remark: current Milestone 2.11.0-M5 has an error and returns NULL for getEngineByName("scala") issues.scala-lang.org/browse/SI-7843 M6 should work (I have not tested)Vibraculum

© 2022 - 2024 — McMap. All rights reserved.