Java Reference
In-Depth Information
the fully qualified class name. As a concrete example, let's access Java's builtin regu‐
lar expression support:
jjs
>
var
pattern
=
java
.
util
.
regex
.
Pattern
.
compile
(
"\\d+"
);
jjs
>
var
myNums
=
pattern
.
split
(
"a1b2c3d4e5f6"
);
jjs
>
print
(
myNums
);
[
Ljava
.
lang
.
String
;
@
10
b48321
jjs
>
print
(
myNums
[
0
]);
a
When we used the REPL to print out the JavaScript variable
myNums
, we got the result
[Ljava.lang.String;@10b48321
—
this is a tell-tale sign that despite being represented in a Java‐
Script variable,
myNums
is really a Java array of strings.
We'll have a great deal more to say about interoperation between Nashorn and Java
later on, but first let's discuss some of the additional features of
jjs
. The general
form of the
jjs
command is:
jjs
[<
options
>]
<
files
>
[--
<
arguments
>]
There are a number of options that can be passed to
jjs
—some of the most com‐
mon are:
•
-cp
or
-classpath
indicates where additional Java classes can be found (to be
used via the
Java.type
mechanism, as we'll see later).
•
-doe
or
-dump-on-error
will produce a full error dump if Nashorn is forced to
exit.
•
-J
is used to pass options to the JVM. For example, if we want to increase the
maximum memory available to the JVM:
$
jjs
-
J
-
Xmx4g
jjs
>
java
.
lang
.
Runtime
.
getRuntime
().
maxMemory
()
3817799680
•
-strict
causes all script and functions to be run in JavaScript strict mode. This
is a feature of JavaScript that was introduced with ECMAScript version 5, and
is intended to reduce bugs and errors. Strict mode is recommended for all new
development in JavaScript, and if you're not familiar with it you should read up
on it.
•
-D
allows the developer to pass key-value pairs to Nashorn as system proper‐
ties, in the usual way for the JVM. For example: