As you can tell by the title of my blog, I’m a Java programmer, and lately I’ve been fucking around with Groovy; it’s pretty damn groovy. It allows you to write almost pseudo Java code, to add a little twist on Java, personally I’ve been using this instead of K-Script because it allows me to interact with my library without needing to code in functions for it.
 
To embed it into your Java application is pretty simple, just download the latest library here then import embedded/groovy-all-2.*.*.jar into your IDE/command line compile, once that’s done you need to create a Groovy script loader, I’ll just give you the class I made, it’s extremely simple.

 

package me.konloch.kontainer.misc;

import java.io.File;

import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;

/*
 * Used for the groovy script embedding
 * 
 * @author Konloch
 */

public class GroovyScriptExecuter {

	private String name;

	public GroovyScriptExecuter(String name) {
		this.name = name;
	}

	public void run(String[] args) throws Exception {
		GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());
		Class<?> groovyClass = loader.parseClass(new File(name));

		// let's call some method on an instance
		GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
		groovyObject.invokeMethod("main", args);
	}

}

Obviously change the package, once that’s done you call on it like this.

		try {
			GroovyScriptExecuter nigger = new GroovyScriptExecuter("test.groovy");
			nigger.run(null);
		} catch(Exception e) {
			e.printStackTrace();
		}

Then create a file called test.groovy, put this inside of that file

 

public class Tester {

    public void main(String[] args) {
        println "that's groovy...";
    }

}

And boom! You’re done, load that, and it’ll split out this

that’s groovy…