-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
28 lines (23 loc) · 900 Bytes
/
Example.java
File metadata and controls
28 lines (23 loc) · 900 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
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.Invocable;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NoSuchMethodException;
public class Example {
private void runJsFile() throws ScriptException, FileNotFoundException {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByExtension("js");
e.eval(new FileReader("./runme.js"));
}
public static void main(String[] args) throws ScriptException, NoSuchMethodException, FileNotFoundException {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByExtension("js");
e.eval("function post(message){ print(message) }");
Invocable i = (Invocable) e;
i.invokeFunction("post","Greetings from Earth.");
Example ex = new Example();
ex.runJsFile();
}
}