JSR-223 Scala Script Engine

前端 未结 4 1439
终归单人心
终归单人心 2021-01-12 12:52

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 fi

相关标签:
4条回答
  • 2021-01-12 13:01

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

    0 讨论(0)
  • 2021-01-12 13:04

    This is a solid ScriptingEngine implementation

    0 讨论(0)
  • 2021-01-12 13:06

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

    0 讨论(0)
  • 2021-01-12 13:13

    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();
      }
    }
    
    0 讨论(0)
提交回复
热议问题