Script writing in Mustang and Rhino:java 6

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

The most recent Java Master version (Java SE 6, also known as Mustang) is now in beta release phase. Although this version is not as much as the Java 5 Update, it does have some interesting new features. Undoubtedly, one of these is support for scripting languages.

Scripting languages such as PHP, Ruby, JavaScript, Python (or Jython) are widely used in many fields and are popular because of their flexibility and simplicity. Because scripts are interpreted rather than compiled, they can be easily run and tested from the command line. This compresses the coding/testing cycle and increases the productivity of developers. Scripts are often typed dynamically and their syntax is extremely expressive, and the algorithms written are much simpler than the equivalent algorithms in Java. It's usually fun to use.

In many cases, scripting languages from Java can be useful, such as providing extensions to Java applications so that users can write their own scripts to extend or customize core functionality. Scripting languages are more readable and easier to write, so (technically) they are the ideal language for providing end users with the possibility of customizing products on demand.

There are already many stand-alone scripting packages available in Java, including Rhino, Jacl, Jython, BeanShell, JRuby, and so on. The new message is that Java 6 provides built-in support for scripting languages through a standard interface.

Java 6 provides full support for the JSR-223 specification. The specification provides a convenient, standard way to execute scripting languages from within Java, and provides the ability to access Java resources and classes from within the script. Java 6 comes with built-in integration with the JavaScript implementations of Mozilla Rhino. Based on this specification, support for other scripting languages such as PHP, groovy, and BeanShell is also in progress. This article is concerned with Rhino implementations, but other languages should be basically the same.

Where do the scripting language names come from? Since most scripting languages come from open source projects, their names are often thought out by their respective authors. The name of Rhino (Rhino) comes from the animals on the cover of the Book O ' Reilly's books on JavaScript. PHP is a shorthand for php:hypertext preprocessor, following the custom of UNIX interpretation. Jython is the Java implementation of the Python scripting language. And groovy is just trying to be cool.

Using the scripting engine

The JSR 223 specification is easy to use. To use a script, you only need to know some key classes. It is mainly the ScriptEngine class, which handles script interpretation and evaluation. To instantiate a script engine, you should use the Scriptenginemanager class to retrieve the ScriptEngine object of the scripting language of interest. Each scripting language has a name. The Mozilla Rhino ECMAScript scripting language (often called JavaScript) uses "JS" for identification.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");

Embedded JavaScript can be used for a variety of purposes. Because it is flexible and easier to configure than hard coded Java, it is often useful to write business rules that change frequently. Use the eval () method to evaluate the script expression. Any variable used in the scripting environment can be assigned from within Java code using the put () method.

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("age", 21);
engine.eval( "if (age >= 18){
  " + " print('Old enough to vote!'); " +
  "} else {"
   + " print ('Back to school!');" +
  "}");
> Old enough to vote!

The eval () method also accepts a reader object, which makes it easy to save the script in a file or other external source, as shown in the following example:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("age", 21);
engine.eval(new FileReader("c:/voting.js"));

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.