Java Reference
In-Depth Information
Since the release of Java SE 6, it has been possible to work with scripting languages
from Java. In this recipe, two solutions were demonstrated for executing JavaScript via
the command line or terminal. In Solution 1, you looked at the jjs command-line
tool, which is new in Java 8. This tool can be used to invoke one or more JavaScript
files, or to start an interactive Nashorn interpreter. In the example, you took a look at
how to invoke a JavaScript file with and without passing arguments. You also took a
look at how to invoke jjs as an interactive interpreter. The tool contains several use-
ful options. To see an entire list refer to the documentation online at ht-
tp://docs.oracle.com/javase/8/docs/technotes/tools/win-
dows/jjs.html . The jjs tool is the desired tool for use with Nashorn because it
contains many more options than the jrunscript tool, which was demonstrated in
Solution 2.
The jrunscript tool was introduced in Java 6 and it allows you to execute
scripts from the command line or invoke an interactive interpreter, similar to jjs . The
difference is that jrunscript also allows you to use other scripting languages by
passing the -l flag, along with the scripting engine name.
jrunscript -l js myTest.js
The jrunscript tool also contains options, but it is limited in comparison to
those available with jjs . To see all of the options available for jrunscript , refer to
the online documentation at http://docs.oracle.com/javase/8/docs/
technotes/tools/windows/jrunscript.html .
18-3. Embedding Expressions in Strings
Problem
You want to refer to expressions or values within a string when invoking JavaScript via
the jjs utility.
Solution
When using Nashorn as a shell scripting language via the jjs tool, it is possible to
embed expressions or values in strings by enclosing them within dollar signs $ and
Search WWH ::




Custom Search