The REPL as a scripting engine
To deal with interoperability with programs written in scripting languages, the Java community process has defined JSR-223, Scripting for the JavaTM Platform, a Java specification request that makes it possible to execute scripts written in other languages (such as Groovy, JavaScript, Ruby, or Jython to name of few) from within a Java program. For instance, we can write a Java program embedding a basic JavaScript snippet as follows:
package com.demo;
import javax.script.*;
public class JSR223Sample {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// expose object as variable to script
engine.put("n", 5);
// evaluate a script string.
engine.eval("for(i=1; i<= n; i++) println(i)");
}
}We will get the following output from the IDE:
run: 1 2 3 4 5 BUILD SUCCESSFUL (total time: 0 seconds)
Starting from...