You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nashorn is a high-performance JavaScript runtime written in Java for the JVM. It allows developers to embed JavaScript code inside their Java applications and even use Java classes and methods from their JavaScript code. You can think it as an alternative to Google's V8 JavaScript engine. It is a successor to Rhino JavaScript runtime which came bundled with earlier JDK versions. Nashorn is written from scratch using new language features like JSR 292(Supporting Dynamically Typed Languages) and `invokedynamic`.
6
+
Nashorn is a high-performance JavaScript runtime written in Java for the JVM. It
7
+
allows developers to embed JavaScript code inside their Java applications and
8
+
even use Java classes and methods from their JavaScript code. You can think it
9
+
as an alternative to Google's V8 JavaScript engine. It is a successor to Rhino
10
+
JavaScript runtime which came bundled with earlier JDK versions. Nashorn is
11
+
written from scratch using new language features like JSR 292(Supporting
12
+
Dynamically Typed Languages) and `invokedynamic`.
7
13
8
14
From the Nashorn documentation:
9
-
> Nashorn uses invokedynamic to implement all of its invocations. If an invocation has a Java object receiver, Nashorn attempts to bind the call to an appropriate Java method instead of a JavaScript function. Nashorn has full discretion about how it resolves methods. As an example, if it can't find a field in the receiver, it looks for an equivalent Java Bean method. The result is completely transparent for calls from JavaScript to Java.
10
15
11
-
Currently, Nashorn supports [ECMAScript 5.1 specification](http://www.ecma-international.org/ecma-262/5.1/) and work is in progress to support [ECMAScript 6](http://www.ecma-international.org/ecma-262/6.0/) as well. Few ECMAScript 6 features like `let` and `const` are available in latest JDK 8 updates(40 and above) and we will cover them later in this chapter.
16
+
> Nashorn uses invokedynamic to implement all of its invocations. If an
17
+
> invocation has a Java object receiver, Nashorn attempts to bind the call to an
18
+
> appropriate Java method instead of a JavaScript function. Nashorn has full
19
+
> discretion about how it resolves methods. As an example, if it can't find a
20
+
> field in the receiver, it looks for an equivalent Java Bean method. The result
21
+
> is completely transparent for calls from JavaScript to Java.
22
+
23
+
Currently, Nashorn supports [ECMAScript 5.1
24
+
specification](http://www.ecma-international.org/ecma-262/5.1/) and work is in
25
+
progress to support [ECMAScript
26
+
6](http://www.ecma-international.org/ecma-262/6.0/) as well. Few ECMAScript 6
27
+
features like `let` and `const` are available in latest JDK 8 updates(40 and
28
+
above) and we will cover them later in this chapter.
12
29
13
30
In this chapter, we will cover the following:
14
31
@@ -23,23 +40,30 @@ In this chapter, we will cover the following:
23
40
24
41
## Working with Nashorn command-line
25
42
26
-
JDK 8 comes bundled with two command-line tools that can be used to work with Nashorn engine. These two command-line tools are `jrunscript` and `jjs`. `jjs` is recommended to be used when working with Nashorn so we will only discuss it. To use `jjs`, you have to add `jjs` to the path. On *nix machines, you can do that adding a symbolic link as shown below.
43
+
JDK 8 comes bundled with two command-line tools that can be used to work with
44
+
Nashorn engine. These two command-line tools are `jrunscript` and `jjs`. `jjs`
45
+
is recommended to be used when working with Nashorn so we will only discuss it.
46
+
To use `jjs`, you have to add `jjs` to the path. On *nix machines, you can do
47
+
that adding a symbolic link as shown below.
27
48
28
49
```bash
29
50
$ cd /usr/bin
30
51
$ ln -s $JAVA_HOME/bin/jjs jjs
31
52
```
53
+
32
54
Windows users can add `$JAVA_HOME/bin` to the path for easy access.
33
55
34
-
Once you have set the symbolic link you can access `jjs` from your terminal. To check version of `jjs`, run the following command.
56
+
Once you have set the symbolic link you can access `jjs` from your terminal. To
57
+
check version of `jjs`, run the following command.
35
58
36
59
```bash
37
60
$ jjs -v
38
61
nashorn 1.8.0_60
39
62
jjs>
40
63
```
41
64
42
-
It will render the version and then show `jjs>` prompt. You can view the full version of `jjs` by using `jjs -fv` command.
65
+
It will render the version and then show `jjs>` prompt. You can view the full
66
+
version of `jjs` by using `jjs -fv` command.
43
67
44
68
To quit the `jjs` shell, you can use `Ctrl-C`.
45
69
@@ -65,7 +89,8 @@ jjs> add(5,10)
65
89
66
90
## Accessing Java classes and methods
67
91
68
-
It is very easy to access Java classes from within Nashorn. Assuming you are inside the `jjs` shell, you can create an instance of HashMap as shown below.
92
+
It is very easy to access Java classes from within Nashorn. Assuming you are
93
+
inside the `jjs` shell, you can create an instance of HashMap as shown below.
In the code shown above we have used `Java` global object to create HashMap object. `Java` global object has `type` method that takes a string with the fully qualified Java class name, and returns the corresponding `JavaClass` function object.
106
+
In the code shown above we have used `Java` global object to create HashMap
107
+
object. `Java` global object has `type` method that takes a string with the
108
+
fully qualified Java class name, and returns the corresponding `JavaClass`
109
+
function object.
82
110
83
111
```bash
84
112
jjs> HashMap
85
113
[JavaClass java.util.HashMap]
86
114
```
87
115
88
-
The `var userAndAge = new HashMap()` is used to instantiate `java.util.HashMap` class using the `new` keyword.
116
+
The `var userAndAge = new HashMap()` is used to instantiate `java.util.HashMap`
117
+
class using the `new` keyword.
89
118
90
-
You can access values by either calling the `get` method or using the `[]` notation as shown below.
119
+
You can access values by either calling the `get` method or using the `[]`
120
+
notation as shown below.
91
121
92
122
```bash
93
123
jjs> userAndAge["shekhar"]
94
124
32
95
125
```
96
126
97
-
Similarly, you can work with other Java collections. To use an `ArrayList` you will write code as shown below.
127
+
Similarly, you can work with other Java collections. To use an `ArrayList` you
128
+
will write code as shown below.
98
129
99
130
```bash
100
131
jjs> var List = Java.type("java.util.ArrayList")
@@ -113,7 +144,8 @@ rahul
113
144
114
145
### Accessing static methods
115
146
116
-
To access static methods you have to first get the Java type using `Java.type` method and then calling method on `JavaClass` function object.
147
+
To access static methods you have to first get the Java type using `Java.type`
148
+
method and then calling method on `JavaClass` function object.
117
149
118
150
```bash
119
151
jjs> var UUID = Java.type("java.util.UUID")
@@ -135,7 +167,10 @@ jjs>
135
167
136
168
## Using external JavaScript libraries
137
169
138
-
Let's suppose we want to use an external JavaScript library in our JavaScript code. Nashorn comes up with a built-in function -- `load` that loads and evaluates a script from a path, URL, or script object. To use `lodash` library we can write code as shown below.
170
+
Let's suppose we want to use an external JavaScript library in our JavaScript
171
+
code. Nashorn comes up with a built-in function -- `load` that loads and
172
+
evaluates a script from a path, URL, or script object. To use `lodash` library
You can use Nashorn extensions that enable users to write scripts that can use Unix shell scripting features. To enable shell scripting features, you have to start `jjs` with `-scripting` option as shown below.
184
+
You can use Nashorn extensions that enable users to write scripts that can use
185
+
Unix shell scripting features. To enable shell scripting features, you have to
186
+
start `jjs` with `-scripting` option as shown below.
150
187
151
188
```bash
152
189
jjs -scripting
@@ -155,7 +192,9 @@ jjs>
155
192
156
193
Now you have access to Nashorn shell scripting global objects.
157
194
158
-
**$ARG:** This global object can be used to access the arguments passed to the script
195
+
**$ARG:** This global object can be used to access the arguments passed to the
196
+
script
197
+
159
198
```
160
199
$ jjs -scripting -- hello hey
161
200
jjs>
@@ -182,7 +221,9 @@ jjs> $EXEC("pwd")
182
221
183
222
### Writing executable scripts
184
223
185
-
You can use shebang(#!) at the beginning of the script to make a script file run as shell executable. Let's write a simple script that reads content of a file. We will use Java's `Files` and `Paths` API.
224
+
You can use shebang(#!) at the beginning of the script to make a script file run
225
+
as shell executable. Let's write a simple script that reads content of a file.
To use Nashorn from inside Java code, you have to create an instance of ScriptEngine from `ScriptEngineManager` as shown below. Once you have `ScriptEngine` you can evaluate expressions.
245
+
To use Nashorn from inside Java code, you have to create an instance of
246
+
ScriptEngine from `ScriptEngineManager` as shown below. Once you have
247
+
`ScriptEngine` you can evaluate expressions.
205
248
206
249
```java
207
250
importjavax.script.ScriptEngine;
@@ -219,7 +262,6 @@ public class NashornExample1 {
219
262
}
220
263
```
221
264
222
-
223
265
Using bindings
224
266
225
267
```java
@@ -247,7 +289,9 @@ public class NashornExample2 {
247
289
248
290
## Using Java 8 features like Streams and Lambdas inside JavaScript code
249
291
250
-
Java 8 supports lambdas and many API in JDK make use of them. Every collection in Java has `forEach` method that accepts a consumer. Consumer is an interface with one method. In Java, you can write following:
292
+
Java 8 supports lambdas and many API in JDK make use of them. Every collection
293
+
in Java has `forEach` method that accepts a consumer. Consumer is an interface
294
+
with one method. In Java, you can write following:
0 commit comments