Chapter 17. The Java Scripting API
The Java Scripting API, introduced in Java SE 6, provides support that
allows Java applications and scripting languages to interact through a
standard interface. This API is detailed in JSR 223, “Scripting for the Java
Platform” and is contained in the javax.script package.
Scripting Languages
Several scripting languages have script engine implementations available that conform to JSR 223. See Scripting Languages (JSR-223 compatible) in Appendix A for a subset of these supported languages.
Script Engine Implementations
The ScriptEngine interface
provides the fundamental methods for the API. The ScriptEngineManager class works in conjunction
with this interface and provides a means to establish the desired
scripting engines to be utilized.
Embedding Scripts into Java
The scripting API includes the ability to embed scripts and/or scripting components into Java applications.
The following example shows two ways to embed scripting components
into a Java application: (1) the scripting engine’s eval method reads in the scripting language
syntax directly, and (2) the scripting engine’s eval method reads the syntax in from a
file.
importjava.io.FileReader;importjava.nio.file.Path;importjava.nio.file.Paths;importjavax.script.ScriptEngine;importjavax.script.ScriptEngineManager;publicclassHelloWorld{publicstaticvoidmain(String[]args)throwsException{ScriptEngineManagerm=newScriptEngineManager();// Sets up Rhino JavaScript Engine.ScriptEngine ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access