Java Reference
In-Depth Information
#! /usr/bin/env
var parameter = $ARG[0];
print(parameter ? "Hello ${parameter}!": "Hello
Nashorn!");
Use the following command to invoke this JavaScript file, passing the parameter
Oracle
:
jjs /src/org/java8recipes/chapter18/js/helloParameter.js
- Oracle
Here is the result:
Hello Oracle!
The
jjs
tool can also be utilized as an interactive interpreter by simply executing
jjs
without any options. The command interpreter allows you to work in a fully inter-
active JavaScript environment. In the following lines of code, the
jjs
tool is invoked
to open a command shell, and a function is declared and executed. Finally, the com-
mand shell is exited.
jjs
jjs> function gallon(width, length, avgDepth){return
(avgDepth * width * length) * 7.48;}
function gallon(width, length, avgDepth){return (avgDepth
* width * length) * 7.48;}
jjs> gallon(16,32,5)
19148.800000000003
jjs> exit()
Solution 2
Make use of the JSR 223
jrunscript
tool to execute JavaScript. To execute a
JavaScript file, invoke the
jrunscript
tool from the command line and pass the
fully qualified name (path included if not in
CLASSPATH
) of a JavaScript file to ex-
ecute. For example, to execute
helloNashorn.js
, use the following command: