My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

Groovy basic example with Java

Groovy makes modern programming features available to Java developers with almost-zero learning curve.It provides the ability to statically type check and statically compile your code for robustness and performance.

Supporting Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain.

Makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL. Increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications. Simplifies testing by supporting unit testing and mocking out-of-the-box. Seamlessly integrates with all existing Java classes and libraries. Compiles straight to Java bytecode so you can use it anywhere you can use Java.

Executing Groovy script using java

As we discussed earlier There are three main approaches for natively integrating Groovy with Java.

package com.javavillage.groovy.GroovyExample;

import java.io.File;

import org.codehaus.groovy.control.CompilerConfiguration;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyShell;
import groovy.util.GroovyScriptEngine;

/**
 * Hello world!
 * 
 */
public class App {
	static void runWithGroovyShell() throws Exception {
		new GroovyShell().parse(new File("src/main/resources/test.groovy"))
				.invokeMethod("hello_world", null);
	}

	@SuppressWarnings("unchecked")
	static void runWithGroovyClassLoader() throws Exception {
		// Declaring a class to conform to a java interface class would get rid
		// of
		// a lot of the reflection here
		Class scriptClass = new GroovyClassLoader().parseClass(new File(
				"src/main/resources/test.groovy"));
		Object scriptInstance = scriptClass.newInstance();
		scriptClass.getDeclaredMethod("hello_world", new Class[] {}).invoke(
				scriptInstance, new Object[] {});
	}

	@SuppressWarnings("unchecked")
	static void runWithGroovyScriptEngine() throws Exception {
		// Declaring a class to conform to a java interface class would get rid
		// of a lot of the reflection here
		Class scriptClass = new GroovyScriptEngine(".")
				.loadScriptByName("src/main/resources/test.groovy");
		Object scriptInstance = scriptClass.newInstance();
		scriptClass.getDeclaredMethod("hello_world", new Class[] {}).invoke(
				scriptInstance, new Object[] {});
	}

	static void runWithCommonBaseCls() {
		new CompilerConfiguration().setScriptBaseClass("ScriptBaseTestScript");
	}

	public static void main(String[] args) {
		try {
			runWithGroovyClassLoader();
			runWithGroovyScriptEngine();
			runWithGroovyShell();
		} catch (Exception e) {			
			e.printStackTrace();
		}

	}
}

groovy script file, placed in src/main/resources(test.groovy)
def hello_world() {
   println "Hello, world!"
}
Maven entries to compile above program.
<dependency>
	<groupId>org.codehaus.groovy</groupId>
	<artifactId>groovy-all</artifactId>
	<version>2.3.1</version>
</dependency>
Output
Hello, world!
Hello, world!
Hello, world!