7

I'm trying to use Scala as a script language, that will be called from java and after that I need to get some objects as a result of script execution.

I tried to find a good interpretor that can do what i need but unsuccesfull. Is the exists an implementation of JSR-223 for Scala? Or may be someone know how to solve my problem. Thanks.

yu.pitomets
  • 1,660
  • 2
  • 17
  • 44
  • 1
    did you see this: http://stackoverflow.com/questions/5654888/how-do-i-set-up-jsr223-scripting-with-scala-as-scripting-language – axel22 Jun 26 '12 at 15:44

4 Answers4

7

Official support in scala starts in version 2.11 as seen in this closed ticket: https://issues.scala-lang.org/browse/SI-874

Oswaldo
  • 513
  • 5
  • 13
3

This library: http://code.google.com/p/scalascriptengine/ may help solve your problem.

Johan Prinsloo
  • 1,188
  • 9
  • 9
0

To be able to run the Codesnippet mentioned in (How do I set up jsr223 scripting with scala as scripting language) 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();
  }
}
Community
  • 1
  • 1
VivaceVivo
  • 831
  • 7
  • 8
0

This is a solid ScriptingEngine implementation

lisak
  • 21,611
  • 40
  • 152
  • 243