Running groovy script in java code

Hello, I am looking to run a groovy script inside Java code, but I have not found many tutorials about this.

I have a line containing a groovy script:

private String processingCode = "def hello_world() { println \"Hello, world!\" }"; 

I also downloaded the groovy SDK.

What groovy jar should be included in the java project? And how to execute a script in Java?

+5
source share
1 answer

You need groovy-all and GroovyShell .

The main class will be:

 package lol; import groovy.lang.GroovyShell; public class Lol { public static void main(String[] args) { String processingCode = "def hello_world() { println 'Hello, world!' }; hello_world();"; GroovyShell shell = new GroovyShell(); shell.evaluate(processingCode); } } 

Here is a demonstration.

Use gradle run to start.

+8
source

Source: https://habr.com/ru/post/1240705/


All Articles