Java SE 6 new features: Support for scripting languages

Source: Internet
Author: User
Tags eval new features java se

At the end of 2006, Sun company released the final official version of Java Standard Edition 6 (Java SE 6), code-named Mustang (Mustang). Mustang has a good performance boost compared to Tiger (Java SE 5). Compared with Tiger's significant enhancements to the API library, although Mustang's new features in the API library appear to be few, it also offers a number of practical and handy features: scripts, webservice,xml, compiler APIs, databases, JMX, networks, and Instrume There are good new features and enhancements in Ntation.

This series of articles focuses on some of the new features of the Java SE 6 in the API library, with examples and explanations to help developers better utilize Java SE 6 in programming practices to improve development efficiency. This is the 6th article in a series that describes the new features of Java SE 6 in scripting.

Java Scripting API Overview

Java SE 6 introduces support for Java Specification Request (JSR) 223, and JSR 223 is designed to define a unified specification that enables Java applications to interact with a variety of scripting engines with a fixed set of interfaces that can be raised on the Java platform The purpose of using various scripting languages. The Javax.script package defines these interfaces, the Java scripting programming API. The Java Script API's goal is similar to the Apache Project Bean script Framework (BSF), through which Java applications can invoke various scripts through virtual machines, while scripting languages can access Java objects and methods in the application. The Java Scripting API is a bridge between the Java platform and the scripting language. First of all, through its numerous existing Java libraries can be used by a variety of scripting languages, saving the development cost to shorten the development cycle; Secondly, some complex and heterogeneous business logic can be given to the scripting language processing, which greatly improves the development efficiency.

There are not many implementation classes defined in the Javax.script package, mainly some interfaces and corresponding abstract classes, and Figure 1 shows the various interfaces and classes contained therein.

Figure 1. Javax.script Package Overview

The fundamental reason for the less specific implementation of this package is that the package simply defines a framework specification for a programming interface, and that the implementation of the specific scripting language on how to parse it is also required by a third party. Although the implementations of these scripting engines vary, these specific implementations are hidden by good isolation for users of the Java scripting API. The Java scripting API provides developers with the following features:

Gets the script input, runs the script through the script engine, and returns the running result, which is the core interface.

Discovery script engine, query script engine information.

Exchange data between scripts and the Java platform through the script engine's run-time context.

Invoke a script function through a Java application.

Before we go into the details of these four features, let's start with a simple example of how to run a script in the Java language, which still starts with the classic Hello world.

Listing 1. Hello World

import javax.script.*;
public class HelloWorld {
  public static void main(String[] args) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    engine.eval("print ('Hello World')");
  }
}

This example is very intuitive, as long as the simplest calls can be done through the two classes of Scriptenginemanager and ScriptEngine. First, the Scriptenginemanager instance creates a ScriptEngine instance, and then the ScriptEngine instance that returns parses the JavaScript script and outputs the running result. Run this program, the terminal will output "Hello world." There may be scriptengine exceptions thrown during the execution of the Eval function, and the reason this exception is thrown is generally caused by incorrect script input syntax. After having a rough idea of the entire API, we can start to introduce the specific features.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.