Java Reference
In-Depth Information
file or evaluate inline JavaScript code by calling on the engine's
eval()
method. In
this example, a JavaScript
print()
statement is passed to the engine.
For more information on using the new Nashorn engine, refer to
Chapter 18
.
2-13.
Executing
JavaScript
from
the
Command-Line
Problem
You want to execute JavaScript via the command-line.
Solution
Invoke the
jjs
tool, which comes as part of Java 8. To execute a JavaScript file, in-
voke the
jjs
tool from the command-line, and then pass the fully qualified name (path
included if not in
CLASSPATH
) of a JavaScript file to execute. For example, to execute
helloNashorn.js
, you use the following command:
jjs /src/org/java8recipes/chapter18/js/helloNashorn.js
Hello Nashorn!
How It Works
The
jjs
command-line tool is new in Java 8. The tool is used to execute JavaScript in-
line or from a JavaScript file. When using the tool inline, you can execute JavaScript
interactively and receive immediate feedback. The tool can execute one or more
.js
files with arguments or without.
To learn more about the
jjs
tool and see a more in-depth example, refer to Recipe
18-2.
Summary