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
Copy file name to clipboardExpand all lines: 10-nashorn.md
+234-1Lines changed: 234 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,11 +14,11 @@ In this chapter, we will cover the following:
14
14
15
15
* Working with Nashorn command-line
16
16
* Accessing Java classes and methods
17
-
* Working with Java collections
18
17
* Using external JavaScript libraries
19
18
* Writing scripts
20
19
* Using Nashorn from Java code
21
20
* Using Java 8 features like Streams and Lambdas inside JavaScript code
21
+
* Turning off Java language access
22
22
23
23
24
24
## Working with Nashorn command-line
@@ -94,6 +94,23 @@ jjs> userAndAge["shekhar"]
94
94
32
95
95
```
96
96
97
+
Similarly, you can work with other Java collections. To use an `ArrayList` you will write code as shown below.
98
+
99
+
```bash
100
+
jjs> var List = Java.type("java.util.ArrayList")
101
+
jjs> var names = new List()
102
+
jjs> names.add("shekhar")
103
+
true
104
+
jjs> names.add("rahul")
105
+
true
106
+
jjs> names.add("sameer")
107
+
true
108
+
jjs> names.get(0)
109
+
shekhar
110
+
jjs> names[1]
111
+
rahul
112
+
```
113
+
97
114
### Accessing static methods
98
115
99
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.
@@ -104,3 +121,219 @@ jjs>
104
121
jjs>UUID.randomUUID().toString()
105
122
e4242b89-0e94-458e-b501-2fc4344d5498
106
123
```
124
+
125
+
You can sort list using `Collections.sort` method as shown below.
126
+
127
+
```bash
128
+
jjs> var Collections = Java.type("java.util.Collections")
129
+
jjs>
130
+
jjs> Collections.sort(names)
131
+
jjs> names
132
+
[rahul, sameer, shekhar]
133
+
jjs>
134
+
```
135
+
136
+
## Using external JavaScript libraries
137
+
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.
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.
150
+
151
+
```bash
152
+
jjs -scripting
153
+
jjs>
154
+
```
155
+
156
+
Now you have access to Nashorn shell scripting global objects.
157
+
158
+
**$ARG:** This global object can be used to access the arguments passed to the script
159
+
```
160
+
$ jjs -scripting -- hello hey
161
+
jjs>
162
+
jjs> $ARG
163
+
hello,hey
164
+
```
165
+
166
+
**$ENV:** A map containing all the current environment variables
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.
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.
## Using Java 8 features like Streams and Lambdas inside JavaScript code
249
+
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:
0 commit comments