1484207149 {100DEFF6} Scripting in Java_ Integrating with Groovy and JavaScript [Sharan 2014-12-04].pdf

(3509 KB) Pobierz
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Author �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xvii
About the Technical Reviewers �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xix
Acknowledgments �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xxi
Introduction �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xxiii
Chapter 1: Getting Started �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
1
Chapter 2: Executing Scripts �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
17
Chapter 3: Passing Parameters to Scripts�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
25
Chapter 4: Writing Scripts in Nashorn �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
51
Chapter 5: Procedures and Compiled Scripts �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
175
Chapter 6: Using Java in Scripting Languages �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
191
Chapter 7: Collections �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
211
Chapter 8: Implementing a Script Engine �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
263
Chapter 9: The jrunscript Command-Line Shell�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
281
Chapter 10: The jjs Command-Line Tool �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
295
Chapter 11: Using JavaFX in Nashorn �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
307
Chapter 12: Java APIs for Nashorn �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
319
Chapter 13: Debugging, Tracing, and Profiling Scripts �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
343
Index �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
355
v
Introduction
When I wrote the three volumes of
Harnessing Java 7
in 2012, I did not include a chapter
on the Java Scripting API because of the limited space that I had for each volume. Note
the phrase “Java Scripting,” which uses two separate words: “Java” and “Scripting.”
“JavaScript” is the name of a scripting language, which has nothing to do with the Java
programming language, whereas the phrase “Java Scripting API” is the Java API that lets
Java applications interact with scripting languages.
In the first release, I had titled this book
Harnessing Scripting in Java,
and it covered
only the Java Scripting API and Rhino script engine. The release did not cover the
JavaScript language. JDK 8 ships with a lightweight high-performance JavaScript engine
called Nashorn, which replaces the Rhino engine that shipped with JDK7. This book
covers the JavaScript language completely with comprehensive coverage of the
Nashorn engine.
Learning the Java Scripting API, which was introduced in Java 6, is not essential
for all Java developers, although it is important and very useful if you are familiar with
scripting languages such as Rhino JavaScript, Groovy, Jython, JRuby, and so on and want
to take advantage of those scripting languages in Java applications.
I started learning the Java Scripting API by reading some online blogs and articles;
they were helpful, but not quite comprehensive in giving me a clear and complete picture
of how the Java Scripting API helps Java applications interact with scripting languages.
My next step in the learning process was reading the specification for JSR-223, Scripting
for the Java Platform Specification, along with the Java API documentation for the
javax.script
package. Reading the JSR-233 specification gave me a complete picture of
the Java Scripting API; however, I was not ready to write this book. I was still missing some
pieces of the puzzle. Therefore, I decided to read the source code for the classes in the
javax.script
package. I also read the source code for some scripting languages. Finally,
in the process of learning the API, I developed a simple scripting engine, which I named
JKScript. Finally, I read the ECMAScript 5.1 specification to get a complete picture of the
JavaScript language itself. The main difficulty that I faced in writing this book was getting
the information on the Nashorn engine and its features. A Wiki is maintained at
https://wiki.openjdk.java.net/display/Nashorn/Main,
which provides information
about the Nashorn engine in bits and pieces. Tying them together in this book with
cohesive examples was a challenge.
It is my sincere hope that readers will enjoy this book and benefit from it.
xxiii
IntroduCtIon
Structure of the Book
This 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 first 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 file.
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. The chapter ends with a program that
shows how to redirect outputs from a script to a file.
Chapter 4, Writing Scripts in Nashorn, covers the JavaScript language in great detail
as described in the ECMAScript 5.1 specification 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. The 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, The jrunscript Command-Line Shell, explains how to use the
jrunscript
command-line shell to execute script using different scripting engines.
Chapter 10, The jjs Command-Line Tool, explains how to use the
jjs
command-line
tool to execute Nashorn scripts in different 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.
xxiv
Zgłoś jeśli naruszono regulamin