O'Reilly logo

Java 7 Pocket Guide, 2nd Edition by Patricia Liguori, Robert Liguori

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

import java.io.FileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class HelloWorld {
  public static void main(String[] args) throws
      Exception {
    ScriptEngineManager m
        = new ScriptEngineManager();
    // Sets up Rhino JavaScript Engine.
    ScriptEngine ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required