JDK6 new features: scripting language Support (scripting)

Source: Internet
Author: User
Tags command line eval new features thread
Overview

JDK6 adds support for scripting languages (JSR 223), which is based on the principle of compiling scripting languages into bytecode, so that the scripting language can also enjoy the many advantages of the Java platform, including portability, security, and so on, because it is now compiled into bytecode and then executed, So it is much more efficient than the original side interpretation. Adding support for scripting languages provides the following benefits for the Java language.

1, many scripting languages have dynamic characteristics, for example, you do not need to use a variable before you declare it, you can use a variable to store completely different types of objects, you do not need to do coercion type conversion, because the conversion is automatic. Now the Java language can also indirectly gain this flexibility through support for scripting languages.

2, can use scripting language to quickly develop product prototypes, because now can be edit-run, without edit-compile-run, of course, because Java has very good IDE support, we can completely edit the source file in the IDE, and then click to run (implied compilation), To achieve the goal of rapid prototyping, so this benefit can be largely ignored.

3, through the introduction of scripting language can easily implement the Java application extension and customization, we can be distributed in the Java application configuration logic, mathematical expressions and business rules extracted, to switch to JavaScript to deal with.

The Sun's JDK6 implementation includes a scripting language engine based on the Mozilla rhino that supports JavaScript, not to say that JDK6 only supports JavaScript, and that any third party can implement a JSR-223-compliant scripting engine on its own Enabling JDK6 to support other scripting languages, such as if you want JDK6 to support Ruby, you can implement a Ruby script engine class yourself according to the JSR 223 specification, specifically, you need to implement javax.script.ScriptEngine (for simplicity, can inherit Javax.script.AbstractScriptEngine) and javax.script.ScriptEngineFactory two interfaces. Of course, before you implement your own scripting language engine, go to scripting.dev.java.net project to see if someone has helped you do your job, so you can use it directly.

Scripting API
The Scripting API is an API for scripting language programs in Java, where the Scripting API is found in Javax.script, and we use this API to write JavaScript programs, This package has a Scriptenginemanager class, which is a portal using the Scripting API, and Scriptenginemanager can find the appropriate scripting engine class via the Jar Service discovery mechanism ( ScriptEngine), the simplest way to use the Scripting API is just three steps below
1. Create a Scriptenginemanager object
2. Obtain ScriptEngine objects through Scriptenginemanager
3, using the ScriptEngine eval method to execute the script

Here is a Hello World program

/** * @author Chinajash * *
public class Helloscript {
public static void Main (Strin [] args) throws Exception {
Scriptenginemanager factory = new Scriptenginemanage ();//step 1
ScriptEngine engine = Factory.getenginebyname ("JavaScript");//step 2
Engine.eval ("Print (' Hello, scripting ')");//step 3
}
}

Run the above program, the console will output hello, scripting above This simple scripting program demonstrates how to run the scripting language inside Java, in addition, we can also use the Scripting API to achieve the following features 1, Exposing Java objects as global variables for scripting languages 2, method to invoke scripting languages in Java 3, scripting languages can implement Java Interface 4, scripting languages can use the classes below the JDK platform like Java to demonstrate the above 4 functions

package scripting;
Import Java.io.file;import javax.script.Invocable;
Import Javax.script.ScriptEngine;
Import Javax.script.ScriptEngineManager;
Import javax.script.ScriptException;
/** * @author Chinajash * *
public class Scriptingapitester {
public static void Main (string[] args) throws Exception {
Scriptenginemanager manager = new Scriptenginemanager ();
ScriptEngine engine = Manager.getenginebyname ("JavaScript");
Testscriptvariables (Engine)//demonstrates how to expose global variables that Java objects are scripting languages
Testinvokescriptmethod (engine);//To demonstrate how to invoke a scripting language in Java
Testscriptinterface (Engine)//Demo script language How to implement Java interface
Testusingjdkclasses (Engine)//Demo scripting language How to use classes under the JDK platform
}
public static void Testscriptvariables (ScriptEngine engine) throws scriptexception{
File File = new file ("Test.txt");
Engine.put ("F", file);
Engine.eval ("println" (' Total Spaces: ' +f.gettotalspace ()) ");
}
public static void Testinvokescriptmethod (ScriptEngine engine) throws exception{
String script = "function Hello (name) {return ' Hello, ' + name;}";
Engine.eval (script);
Invocable INV = (invocable) engine;
string res = (string) inv.invokefunction ("Hello", "scripting");
System.out.println ("Res:" +res);
}
public static void Testscriptinterface (ScriptEngine engine) throws scriptexception{
String script = "var obj = new Object ();"
Obj.run = function () {println (' Run method called ');
}";
Engine.eval (script);
Object obj = engine.get ("obj");
Invocable INV = (invocable) engine;
Runnable r = inv.getinterface (Obj,runnable.class);
Thread th = new Thread (r);
Th.start ();
}
public static void Testusingjdkclasses (ScriptEngine engine) throws exception{
Packages is a global variable in the scripting language that is designed to access JDK package
String js = "function doswing (t) {var f=new Packages.javax.swing.JFrame (t);
F.setsize (400,300); f.setvisible (true);} ";
Engine.eval (JS);
Invocable INV = (invocable) engine;
Inv.invokefunction ("doswing", "Scripting Swing");
}
}
Scripting Tool

There is a command-line tool in the JDK6 provided by sun?? Jrunscript, you can find this tool under <jdk6_home>/bin, Jrunscript is a scripting language interpreter that is independent of the scripting language, but by default it is JavaScript, We can use Jrunscript to test whether the scripting language we write is correct, and here is a simple example of running Jrunscript at the command line
Jrunscript
Js>println ("Hello,jrunscript");
Hello,jrunscript
Js>9*8
72.0
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.