Skip to content

Instantly share code, notes, and snippets.

@schup
Created April 16, 2013 17:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schup/5397811 to your computer and use it in GitHub Desktop.
Save schup/5397811 to your computer and use it in GitHub Desktop.
Implementing an Interface in Groovy and using it in Java through javax.script / JSR-223
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();
}
}
}
@11mad11
Copy link

11mad11 commented Apr 5, 2018

How do we handle pakage if, for exemple, Engine was in com.somePakage.Engine.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment