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.