Skip to content

Instantly share code, notes, and snippets.

@claudemartin
Created July 3, 2017 14:09
Show Gist options
  • Save claudemartin/d761f69e771d71319f20bda980f41b7e to your computer and use it in GitHub Desktop.
Save claudemartin/d761f69e771d71319f20bda980f41b7e to your computer and use it in GitHub Desktop.
package ch.claude_martin.playground;
import java.io.IOException;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class Nashorn {
public static void main(String[] args) throws IOException, ScriptException {
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("nashorn");
Bindings bindings = new SimpleBindings();
engine.eval("function sum(a, b) { return a + b; }", bindings);
int a = 2;
int b = 40;
long c;
c = (long) engine.eval("sum(" + a + ", " + b + ");", bindings);
System.out.println(c);
bindings.put("a", a);
bindings.put("b", b);
c = (long) (double) engine.eval("sum(a,b);", bindings);
System.out.println(c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment