-
-
Save jxc876/657e170f9273d7040f17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def start(){ | |
println "Start the engines!" | |
} | |
def stop(){ | |
println "Stop at once!" | |
} | |
this as Engine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Engine.java | |
public interface Engine { | |
void start(); | |
void stop(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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