如何使用scala作为脚本语言设置jsr223脚本 [英] How do I set up jsr223 scripting with scala as scripting language
问题描述
到目前为止,我已尝试使用scala的jsr223脚本的sling实现,但无法正确设置它。
当我这样做时:
So far I have tried the sling implementation for jsr223 scripting for scala, but was not able to get it set up 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();
}
}
我什么也没得到:
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
和
可能还有另一个我不知道的实施。
maybe there is another Implementation that I'm not aware of.
任何帮助表示赞赏
推荐答案
查看 Apache Sling 的工作示例。脚本及其入口点(即对象)需要遵循某些约定。如果以后需要,我会提供更多相关信息。
Have a look at the test cases in the scala/script module of Apache Sling for a working example. The script and its entry point (that is the object) need to follow certain conventions. I'll provide more information on these if required later.
有关脚本引擎的一般概述,请参阅我的会话从Scala Days 2010开始滑动。
For a general overview of the scripting engine see my session slides from Scala Days 2010.
更新:脚本必须是以下形式:
Update: Scripts must be of the following form:
package my.cool.script {
class foo(args: fooArgs) {
import args._ // import the bindings
println("bar:" + bar)
}
}
args
的类型是由脚本引擎生成,并以附加Args的脚本的简单类名命名。此外,该示例假定 Bindings 已通过用于脚本评估包含名称bar的值。有关详细信息,请参阅 ScalaScriptEngine
。
The type of args
is generated by the script engine and is named after the simple class name of the script appended with 'Args'. Further the example assumes, that the Bindings passed for script evaluation contains a value for the name 'bar'. For further details see the class comment on ScalaScriptEngine
.
你需要将脚本类的名称传递给脚本引擎。您可以通过将完全限定的脚本名称(即 my.cool.script.foo
)放入 ScriptContext
。
You need to pass the name of your script class to the script engine. You do this by putting the fully qualified script name (i.e. my.cool.script.foo
) into the ScriptContext
by the name 'scala.script.class'.
这篇关于如何使用scala作为脚本语言设置jsr223脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!