42

我有一个 groovy 脚本,我想在 java 中执行它。有人可以为我提供有关如何实现的更多文档/示例吗?

4

1 回答 1

66

基本的 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);

有关从 Java 调用 Groovy 的更多方法,请参阅本文

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

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.4.8</version>
</dependency>
于 2013-07-22T14:37:47.750 回答