How to configure jsr223 scripting using scala as a scripting language

So far, I tried the sling implementation for jsr223 scripts for scala, but could not configure it correctly. when i do this:

public static void main(String[] args) { try { new ScriptEngineManager().getEngineByName("scala"). eval("object HelloWorld {def main(args: Array[String]) { println(\"Hello, world!\") }}"); } catch (ScriptException e) { e.printStackTrace(); } } 

I have nothing:

 javax.script.ScriptException: ERROR org.apache.sling.scripting.scala.Script line 13 : not found: type Script at org.apache.sling.scripting.scala.ScalaScriptEngine.eval(ScalaScriptEngine.scala:117) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247) 

similar problems are discussed here: http://scala-programming-language.1934581.n4.nabble.com/How-to-compile-Scala-code-from-java-using-the-current-ClassLoader-instead-of-a -string-based-classpat-td1955873.html # a1955873

and

http://dev.day.com/discussion-groups/content/lists/sling-dev/2009-12/2009-12-01_Scala_scripting_support_was_Re_And_another_one____Michael_D_rig.html

maybe there is another implementation that I don't know about.

Any help appreciated

+6
source share
3 answers

Take a look at the test cases in the scala / script module of Apache Sling for a working example. The script and its entry point (i.e. the object) must follow certain conventions. I will provide more information about this if needed later.

For a general overview of the script engine, see my session slides from Scala Days 2010 .

Update: Scripts should have the following form:

 package my.cool.script { class foo(args: fooArgs) { import args._ // import the bindings println("bar:" + bar) } } 

The args type is generated by the script engine and named after the simple name of the script class included with "Args". The following example assumes that the Bindings passed to evaluate the script contain a value for the name 'bar'. See the comment on the ScalaScriptEngine class for more information.

You need to pass the name of your script class to the script engine. You do this by putting the full script name (i.e. my.cool.script.foo ) in a ScriptContext named 'scala. script.class.

+6
source

With the completion of https://issues.scala-lang.org/browse/SI-874 in version 2.11, this should be as simple as shown on the ticket:

 import javax.script.*; ScriptEngine e = new ScriptEngineManager().getEngineByName("scala"); e.getContext().setAttribute("label", new Integer(4), ScriptContext.ENGINE_SCOPE); try { engine.eval("println(2+label)"); } catch (ScriptException ex) { ex.printStackTrace(); } 
+4
source

Unfortunately, my comment could not be read without breaking lines - so ...

To be able to run the aforementioned Codesnippet, 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

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


All Articles