
■ IntroduCtIon
xxiv
Structure of the Book
is book contains 13 chapters: Each chapter introduces a new topic from the Java
Scripting API and the JavaScript language. Each chapter makes use of the material
covered in the previous chapters.
Chapter 1, Getting Started, introduces you to the Java Scripting API and
demonstrates how to write your rst Java program to execute a script written in Nashorn.
It walks through the steps that are needed to download and install other scripting
languages such as Groovy, Jython, and JRuby. Finally, this chapter takes a brief tour of the
classes and interfaces contained in the javax.script package, describing their usages
and relationship with other classes.
Chapter 2, Executing Scripts, shows how to execute a script, which is stored in a le.
It also demonstrates how to pass parameters from a Java application to a scripting engine,
and vice versa.
Chapter 3, Passing Parameter to Scripts, discusses advanced techniques and all the
internal setups that are involved in passing parameters between a Java application and
a scripting engine. It starts with a detailed description of the terms binding, scope, and
script context. Later, it explains in detail with several illustrations how bindings, scope,
and script context work together in the Java Scripting API. Each explanation is paired with
a snippet of code, a complete program, or both. e chapter ends with a program that
shows how to redirect outputs from a script to a le.
Chapter 4, Writing Scripts in Nashorn, covers the JavaScript language in great detail
as described in the ECMAScript 5.1 specication that is supported by the Nashorn engine.
Chapter 5, Procedures and Compiled Scripts, explains how to invoke top-level
procedures, functions, and object-level methods written in a scripting language from a
Java application. It explains how to compile scripts in intermediate form, if supported by
the scripting engine, and execute them repeatedly.
Chapter 6, Using Java in Nashorn, explains how to use features and constructs of
the Java programming language inside scripting languages. e Java features discussed
are importing Java classes, creating Java objects, using Java overloaded methods,
implementing Java interfaces, and so on.
Chapter 7, Collections, explains untyped and typed arrays in Nashorn. It also
explains how to use Java collection classes such as java.util.Set and java.util.Map
in Nashorn.
Chapter 8, Implementing a Script Engine, discusses the steps needed to implement
a new script engine. It explains all classes and interfaces in the Java Scripting API that
are involved in creating a new script engine. It walks through the deployment setup that
makes a script engine autodiscoverable by a script engine manager. In the process, it
creates a simple script engine, which we call JKScript, which is capable of executing an
arithmetic expression.
Chapter 9, e jrunscript Command-Line Shell, explains how to use the jrunscript
command-line shell to execute script using dierent scripting engines.
Chapter 10, e jjs Command-Line Tool, explains how to use the jjs command-line
tool to execute Nashorn scripts in dierent modes. It walks through the syntax and the
options for the command-line tool. Several examples are provided to illustrate the usage
of the shell.