Created
July 3, 2017 14:09
-
-
Save claudemartin/d761f69e771d71319f20bda980f41b7e 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
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