forked from hacker85/JavaLessons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptingLesson.java
More file actions
28 lines (25 loc) · 932 Bytes
/
ScriptingLesson.java
File metadata and controls
28 lines (25 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package anotation;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* Created by max on 2/18/17.
*/
public class ScriptingLesson {
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.put("k", 4);
String scriptString = "var n = 1;" +
"function f(a) {return a + 4;};" +
"print('hello');" +
"var q = 5;" +
"1 + 2 + k;";
Object result = engine.eval(scriptString);
System.out.println(engine.get("q"));
System.out.println(result);
Object funcResult = ((Invocable)engine).invokeFunction("f", 1);
System.out.println(funcResult);
}
}