首页 文章

如何在java中运行groovy脚本?

提问于
浏览
28

我有一个groovy脚本,我想在java中执行它 . 有人可以向我提供有关如何实现这一目标的进一步文档/示例吗?

1 回答

  • 49

    基本Java Groovy集成:

    // call groovy expressions from Java code
    Binding binding = new Binding();
    binding.setVariable("foo", new Integer(2));
    GroovyShell shell = new GroovyShell(binding);
    
    Object value = shell.evaluate(groovyScript);
    

    See this article for more ways to call Groovy from Java

    PS: 您需要包含 groovy-all-m.n.m.jar ,例如 groovy-all-2.1.6.jar 在Java程序中,for example

    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.8</version>
    </dependency>
    

相关问题