Java Reference
In-Depth Information
Using a scripting language in a Java application provides several advantages:
Most scripting languages are dynamically typed, which makes it
simpler to write programs.
They provide a quicker way to develop and test small applications.
Customization by end users is possible.
A scripting language may provide domain-specific features that
are not available in Java.
Scripting languages have some disadvantages as well. For example, dynamic typing
is good to write simpler code; however, it turns into a disadvantage when a type is
interpreted incorrectly and you have to spend a lot of time debugging it.
Scripting support in Java lets you take advantage of both worlds: it allows you to
use the Java programming language for developing statically typed, scalable, and
high-performance parts of the application and use a scripting language that fits the
domain-specific needs for other parts.
I will use the term script engine frequently in this topic. A script engine is a software
component that executes programs written in a particular scripting language. Typically,
but not necessarily, a script engine is an implementation of an interpreter for a scripting
language. Interpreters for several scripting languages have been implemented in Java.
They expose programming interfaces so a Java program may interact with them.
JDK 7 was cobundled with a script engine called Rhino JavaScript. JDK 8 replaced the
Rhino JavaScript engine with a lightweight, faster script engine called Nashorn JavaScript.
This topic discusses Nashorn JavaScript, not Rhino JavaScript. Please visit
www.mozilla.org/rhino for more details on Rhino JavaScript documentation. If you want to
migrate programs written with Rhino JavaScript to Nashorn, please visit the Rhino Migration
Guide at https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide .
If you are interested in using Rhino JavaScript with JDK 8, visit the page at
https://wiki.openjdk.java.net/display/Nashorn/Using+Rhino+JSR-223+engine+with+JDK8 .
Java includes a command-line shell called jrunscript that can be used to run
scripts in an interactive mode or a batch mode. The jrunscript shell is scripting-
language-neutral; the default language is Rhino JavaScript in JDK 7 and Nashorn in JDK 8.
I will discuss the jrunscript shell in detail in Chapter 9. JDK 8 includes another
command-line tool called jjs that invokes the Nashorn engine and offers Nashorn-specific
command-line options. If you are using Nashorn, you should use the jjs command-line
tool over jrunscript . I will discuss the jjs command-line tool in Chapter 10.
Java can execute scripts in any scripting language that provides an implementation
for a script engine. For example, Java can execute scripts written in Nashorn JavaScript,
Rhino JavaScript, Groovy, Jython, JRuby, and so on. Examples in this topic use Nashorn
JavaScript language.
In this topic, the terms “Nashorn,” “Nashorn Engine,” “Nashorn JavaScript,” “Nashorn
JavaScript Engine,” “Nashorn Scripting Language,” and “JavaScript” have been used
synonymously.
 
Search WWH ::




Custom Search