Java Reference
In-Depth Information
The echo() function is the same as the print() function, except that it works only in
scripting mode. Scripting mode is discussed in Chapter 10.
Using Other Scripting Languages
It is very simple to use a scripting language, other than Nashorn, in a Java program. You
need to perform only one task before you can use a script engine: include the JAR files
for a particular script engine in your application CLASSPATH. Implementers of script
engines provide those JAR files.
Java uses a discovery mechanism to list all script engines whose JAR
files have been included in the application CLASSPATH. An instance of the
ScriptEngineFactory interface is used to create and describe a script engine. The
provider of a script engine provides an implementation for the ScriptEngineFactory
interface. The getEngineFactories() method of the ScriptEngineManager
returns a List<ScriptEngineFactory> of all available script engines factories. The
getScriptEngine() method of the ScriptEngineFactory returns an instance of the
ScriptEngine . Several other methods of the factory return metadata about the engine.
Table 1-1 lists details on how to install script engines before you can use them in your
Java application. The list of websites and instructions are valid at the time of this writing;
they may become invalid at the time of reading. However, they show you how a script
engine for a scripting language is installed. If you are interested in using Nashorn, you do
not need to install anything on your machine. Nashorn is available in JDK 8.
Table 1-1. Installation Details for Installing Some Script Engines
Script Engine
Version
Website
Installation Instructions
groovy.
codehaus.org
Groovy
2.3
Download the installation file for
Groovy; it's a ZIP file. Unzip it. Look for
a JAR file named groovy-all-2.0.0-
rc-2.jar in the embeddable folder. Add
this JAR file to the CLASSPATH.
www.jython.org
Jython
2.5.3
Download the Jython installer file that
is a JAR file. Extract the jython.jar file
and add it to the CLASSPATH.
www.jruby.org
JRuby
1.7.13
Download the JRuby installation file.
You have an option to download a ZIP
file. Unzip it. In the lib folder, you will
find a jruby.jar file that you need to
include in the CLASSPATH.
Listing 1-3 shows how to print details of all available script engines. The output
shows that the script engine for Groovy, Jython, and JRuby are available. They are
available because I have added the JAR files for their engines to the CLASSPATH on my
 
 
Search WWH ::




Custom Search