forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNashorn1.java
More file actions
31 lines (25 loc) · 933 Bytes
/
Nashorn1.java
File metadata and controls
31 lines (25 loc) · 933 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
29
30
31
package com.winterbe.java8;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.time.LocalDateTime;
import java.util.Date;
/**
* Calling javascript functions from java with nashorn.
*
* @author Benjamin Winterberg
*/
public class Nashorn1 {
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("res/nashorn1.js"));
Invocable invocable = (Invocable) engine;
Object result = invocable.invokeFunction("fun1", "Peter Parker");
System.out.println(result);
System.out.println(result.getClass());
invocable.invokeFunction("fun2", new Date());
invocable.invokeFunction("fun2", LocalDateTime.now());
invocable.invokeFunction("fun2", new Person());
}
}