JSR-223 Scala Script Engine

I am 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 the script.

I tried to find a good interpreter who could do what I needed, but unsuccessfully. Is there a JSR-223 implementation for Scala? Or maybe someone knows how to solve my problem. Thanks.

+6
source share
4 answers

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

+7
source

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

+3
source

In order to run Codesnippet, mentioned in ( How to configure jsr223 scripting using scala as a 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
source

This is a solid expression of ScriptingEngine

0
source

Source: https://habr.com/ru/post/919045/


All Articles