Java SE 6 scripting engine makes the program powerful

Source: Internet
Author: User
Tags what scripting java se

Now that Java SE 5 has been released, Java SE 6 will be released next year. Java SE 6 is much better than the Java SE5, it is more powerful and designed for Vista, which means that Java SE 6 will be the best choice for Vista. and the largest number of new features provided by Java SE 6 will be its biggest selling point.

One of the most compelling new features of Java SE 6 is the embedded scripting support. By default, Java SE 6 only supports JavaScript, but this does not mean that Java SE 6 can only support JavaScript. Some interfaces are provided in Java SE 6 to define a scripting specification, namely JSR223. By implementing these interfaces, Java SE 6 can support any scripting language (such as PHP or Ruby).

Run the first script program

Before you run a script using Java SE 6, you must know what scripting language your Java SE 6 supports. There are many classes in the Javax.script package, but the most important of these classes is Scriptenginemanager. This class allows you to get all of the scripts supported by the current Java SE 6. The following example will list all the scripting engine factories that can be used.

import javax.script.*;
import java.io.*;
import java.util.*;
import static java.lang.system.*;
public class Listscriptengines
{
public static void Main (String args[])
{
Scriptenginemanager Manager = new Scriptenginemanager ();   
//Get all the scripting engine factory
list<scriptenginefactory> factories = manager.getenginefactories ();
This is the new for statement syntax
for (scriptenginefactory factory:factories)
{
//Print script information
out of Java SE 5 and Java SE 6.     printf ("Name:%s%n" +
"version:%s%n" +
"Language Name:%s%n" +
"Language version:%s%n" +
"Extensions:%s%n" +
Mime types:%s%n "+
" Names:%s%n ",
Factory.getenginename (),
Factory . Getengineversion (),
Factory.getlanguagename (),
Factory.getlanguageversion (),
Factory.getextension    S (),
Factory.getmimetypes (),
Factory.getnames ();
Get the current scripting engine
ScriptEngine engine = Factory.getscriptenGine ();
}
}
}

The example above must be compiled in Java SE 6. Where the import static java.lang.system.* is the new syntax, references all static members in the System and can be used directly with out, in, or err.

By running the Java Listscriptengines, the following information is displayed

Name: Mozilla Rhino
Version: 1.6 release 2
Language name: ECMAScript
Language version: 1.6
Extensions: [js]
Mime types: [application/javascript, application/ecmascript, text/javascript, text/ecmascript]
Names: [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]

The bottom line is the alias for the script, which means any one of them can be used. There are 3 different ways to get a specific scripting engine.

• Script engine based on extension

ScriptEngine engine = manager.getEngineByExtension("js");

The getenginebyextension parameter is EXTENSIONS:[JS] [...] Part of it.

• Script engine based on MIME type

ScriptEngine engine = manager.getEngineByMimeType("text/javascript");

Getenginebymimetype parameters can be MIME types: [Application/javascript, Application/ecmascript, Text/javascript, text/ Any one of the ECMAScript] can change the text/javascript to Text/ecmascript.

• Get script engine by name

ScriptEngine engine = manager.getEngineByName("javascript");

The getenginebyname parameters can be names: [JS, Rhino, JavaScript, JavaScript, ECMAScript, ECMAScript], If you can change JavaScript to ECMAScript.

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.