如何使用 scala 作为脚本语言设置 jsr223 脚本

发布于 2024-11-01 16:46:22 字数 1521 浏览 9 评论 0 原文

到目前为止,我已经尝试了 scala 的 jsr223 脚本的 sling 实现,但无法正确设置它。 当我这样做时:

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)

这里讨论了类似的问题: 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

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

也许还有另一个我不知道的实现。

任何帮助表示赞赏

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();
    }
}

I got nothing but:

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'm not aware of.

Any help appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三岁铭 2024-11-08 16:46:22

查看 Apache Sling 作为一个工作示例。脚本及其入口点(即对象)需要遵循某些约定。如果稍后需要,我将提供有关这些的更多信息。

有关脚本引擎的总体概述,请参阅我的 Scala Days 2010 会议幻灯片

更新:脚本必须采用以下形式:

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

args的类型为Bindings 通过脚本评估包含名称“bar”的值。有关更多详细信息,请参阅 ScalaScriptEngine

您需要将脚本类的名称传递给脚本引擎。您可以通过将完全限定的脚本名称(即 my.cool.script.foo)放入 ScriptContext,名称为“scala.script.class”。

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.

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)
  }
}

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.

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'.

寻找我们的幸福 2024-11-08 16:46:22

随着 https://issues.scala-lang.org/browse/SI-874 的结论 在 2.11 版本中,它应该像票证中所示的那样简单:

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();
}

With the conclusion of https://issues.scala-lang.org/browse/SI-874 in version 2.11, it should be as easy as what is shown in 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();
}
如果没结果 2024-11-08 16:46:22

不幸的是,如果没有换行符,我的评论将无法阅读 - 所以...

为了能够运行提到的 Codesnippet,我需要进行以下更改。
我用的是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();
  }
}

Unfortunately my comment was unreadable without linebreaks - so...

To be able to run the Codesnippet mentioned 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();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文