yu.pitomets
yu.pitomets

Reputation: 1840

JSR-223 Scala Script Engine

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.

Upvotes: 7

Views: 5562

Answers (4)

lisak
lisak

Reputation: 21971

This is a solid ScriptingEngine implementation

Upvotes: 0

VivaceVivo
VivaceVivo

Reputation: 931

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();
  }
}

Upvotes: 0

Oswaldo
Oswaldo

Reputation: 523

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

Upvotes: 7

Johan Prinsloo
Johan Prinsloo

Reputation: 1188

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

Upvotes: 3

Related Questions