Home

This Chapter
-Chapter 3: Scripting
-Core Types
-Listing All Script Engines
-Running Scripts
-Binding Scripts
-Using Invocable
-Using Compilable
-Summary

Table of Contents
-Introduction
-Chapter 1: Core Libraries
-Chapter 2: Dynamic Compilation
-Chapter 3: Scripting
-Chapter 4: Networking
-Chapter 5: Swing Updates
-Chapter 6: Abstract Window Toolkit
-Chapter 7: Internationalization
-Chapter 8: Java Database Connectivity 4.0
-Chapter 9: XML Digital Signature API
-Chapter 10: Streaming API for XML
-Chapter 11: Java Architecture for XML Binding
-Chapter 12: Web Services
-Chapter 13: JavaBeans Activation Framework
-Chapter 14: User-Defined MXBeans
-Chapter 15: Concurrency Updates
-Appendix A: Enums
-Appendix B: Generics
-Appendix C: Annotations

Previous
Next

 

Chapter 3

Scripting

JSR 223, Scripting for the Java Platform describes various mechanisms for allowing scripting language programs to access information in the Java platform and permitting scripting language pages to be used in a Java server-side application. The concept of enabling communication between scripts and program objects itself is not new. A notable example is how JavaScript, the scripting language supported by most browsers, can access methods in Java applets or Flash programs. A non-Java example: VBScript that can be used to access ActiveX objects inside Microsoft Office applications.

Note

The JSR 223 documentation can be downloaded from http://jcp.org/en/jsr/detail?id=223

JSR 223 defines a standard on how to do this kind of communication with multiple script languages. Java 6 provides a script engine based on Rhino, an open source implementation of JavaScript. Rhino is written in Java and can be downloaded from http://www.mozilla.org/rhino. Support for other scripting languages can be found in the Scripting project at java.net (https://scripting.dev.java.net). The languages supported so far in this project include Groovy, Java, Jelly, Jexl, JudoScript, OGNL, Pnuts, Python, Ruby, Scheme, Sleep, Tcl, xpath, and XSLT. This chapter concentrates on the JavaScript engine in Java 6.

The Scripting API is defined and implemented as types in the javax.script package. This package offers the following areas of functionality.

  1. Script execution. This feature allows Java programmers to run scripts written in a scripting language for which an engine is available. For Java 6, only JavaScript scripts are supported.
  2. Binding. This unit of functionality enables Java programmers to access Java objects from script programs. Those Java objects must first be bound to variables.
  3. Compilation. Before scripts can be executed, the corresponding script engine must first compile the script into intermediate code. This feature allows the storage of such intermediate code so that scripts that are invoked repeatedly need only be compiled once, hence speeding up the whole execution process.
  4. Invocation. This feature is related to compilation. However, invocation enables intermediate code to be reused. The difference is very subtle. Compilation allows the whole script to be re-executed and invocation allows individual procedures/functions in the script to be re-executed.
  5. Script engine discovery and metadata. For a scripting language to interact with Java, a script engine for that language must be available. The Scripting API allows script engines to be registered and discovered at run time. In addition, you can also query attributes about registered script engines.

The rest of this section takes a look at the core types in the javax.script package and provide several examples.

Previous
Next