2016-01-15 7 views
5

Hallo Ich suche eine groovige Skript in Java-Code auszuführen, aber ich habe nicht viele Tutorial darüber finden.Lauf groovy Skript in Java-Code

Ich habe einen String, der ein starkes Skript enthalten:

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

Ich habe auch die Groovy SDK heruntergeladen.

Welche groovy jar sollte ich in Java-Projekt enthalten? Und wie wird das Skript in Java ausgeführt?

Antwort

8

Was Sie brauchen, ist eine groovy-all Abhängigkeit und GroovyShell.

Hauptklasse wird:

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 ist eine Demo.

Verwenden gradle run um es auszuführen.

+0

Vielen Dank Mann :) –

+0

Es gibt keine groovy-all in meinem groovy-sdk/lib sollte ich das ganze sdk enthalten? –

+0

Ich denke, alles, was Sie brauchen, finden Sie unter '$ GROOVY_HOME/lib'. – Opal

Verwandte Themen