42

I have a groovy script and i want to execute it in java. Could someone provide me with further documentation / examples on how this is possible?

1
  • 1
    This question is very much on topic with groovy-java integration. Answer below is a starting point. Commented May 28, 2016 at 16:13

1 Answer 1

70

Basic Java+Groovy Integration:

// 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: You need to include groovy-all-m.n.m.jar e.g. groovy-all-2.1.6.jar in your Java program, for example:

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.4.8</version>
</dependency>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks anubhava. Solved my purpose. I have been able to call groovy script from java source file. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.