Java Reference
In-Depth Information
try {
// Execute the script
engine.eval(script);
}
catch (ScriptException e) {
e.printStackTrace();
}
}
}
Hello Scripting!
Using the jjs Command-line Tool
In the previous section, you saw how use the Nashorn scripting engine from the Java
programs. In this section, I will show you how to perform the same task using the
jjs
command-line tool. The tool is stored in the
JDK_HOME\bin
and
JRE_HOME\bin
directories.
For example, if you have installed JDK8 in
C:\java8
directory on Windows, the path for
the
jjs
tool will be
C:\java8\bin\jjs.exe
. The
jjs
tool can be used to execute Nashorn
script in a file or execute scripts interactively.
The following is an invocation of the
jjs
tool on a Windows command-prompt. The
script is entered and executed. You can use q
uit()
or
exit()
function to exit the
jjs
tool:
C:\>jjs
jjs> print('Hello Scripting!');
Hello Scripting!
jjs> quit()
C:\>
You may get the following error executing the
jjs
command:
'jjs' is not recognized as an internal or external command, operable program
or batch file.
The error indicates that the command prompt was not able to locate the
jjs
tool. In
this case, you can either enter the full path of the
jjs
tool or add the directory containing
the too in the system PATH.
Consider the code listed in Listing 1-2. It is Nashorn code that uses the
print()
function to print a message on the standard output. The code is saved in a file named
helloscripting.js
.
Search WWH ::
Custom Search