简体   繁体   English

如何在java中运行groovy脚本?

[英]How to run groovy script in java?

I have a groovy script and i want to execute it in java. 我有一个groovy脚本,我想在java中执行它。 Could someone provide me with further documentation / examples on how this is possible? 有人可以向我提供有关如何实现这一目标的进一步文档/示例吗?

Basic Java+Groovy Integration: 基本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 有关从Java调用Groovy的更多方法,请参阅此文章

PS: You need to include groovy-all-mnmjar eg groovy-all-2.1.6.jar in your Java program, for example : PS:您需要在Java程序中包含groovy-all-mnmjar例如groovy-all-2.1.6.jar例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM