Java Reference
In-Depth Information
Summary
A scripting language is a programming language that provides you the ability to write
scripts that are evaluated (or interpreted) by a runtime environment called a script engine
(or an interpreter). A script is a sequence of characters that is written using the syntax of a
scripting language and used as the source for a program executed by an interpreter. The
Java Scripting API allows you to execute scripts written in any scripting language that can
be compiled to Java bytecode from the Java application. JDK 6 and 7 shipped with a script
engine called Rhino JavaScript engine. In JDK 8, the Rhino JavaScript engine has been
replaced with a script engine called Nashorn.
Nashorn engine can be used in two ways; it can be embedded into the JVM and
invoked from the Java programs directly or it can be invoked from a command prompt
using the
jjs
command-line tool.
Nashorn provides three functions to print text on the standard output:
print()
,
printf()
, and
echo()
. The
print()
function takes a variable number of arguments; it
prints all arguments separating them with a space and at the end, prints a new line. The
printf()
function prints formatted text; it works the same way the printf-style formatting
works in the Java programming language. The
echo()
function is works the same way the
print()
function works, except that the former is available only when Nashorn engine is
invoked in scripting mode.
Scripts are executed using a script engine that is an instance of the
ScriptEngine
interface. The implementer of the
ScriptEngine
interface also provides an
implementation of the
ScriptEngineFactory
interface whose job is to create instances of
the script engine and provide details about the script engine. The
ScriptEngineManager
class provides a discovery and instantiation mechanism for script engines. A
ScriptManager
maintains a mapping of key-value pairs as an instance of the
Bindings
interface that is shared by all script engines that it creates.
Search WWH ::
Custom Search