Skip to content

Instantly share code, notes, and snippets.

@jxc876
Forked from schup/Engine.java
Last active August 29, 2015 14:10
Show Gist options
  • Save jxc876/657e170f9273d7040f17 to your computer and use it in GitHub Desktop.
Save jxc876/657e170f9273d7040f17 to your computer and use it in GitHub Desktop.
def start(){
println "Start the engines!"
}
def stop(){
println "Stop at once!"
}
this as Engine
// Engine.java
public interface Engine {
void start();
void stop();
}
// invoke the script using JSR
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
public class Main {
public static void main(String[] args) {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine groovy = scriptEngineManager.getEngineByExtension("groovy");
try (Reader reader = new FileReader(new File("engine.groovy"))) {
ScriptContext scriptContext = new SimpleScriptContext();
Engine result = (Engine) groovy.eval(reader, scriptContext);
result.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment