JSR-223 Scala Script Engine

前端 未结 4 1481
终归单人心
终归单人心 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: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();
      }
    }
    

提交回复
热议问题