20050809 星期二 2005年08月09日

Scripting for Java

JSR223 "describe mechanisms allowing scripting language programs to access information developed in the Java Platform and allowing scripting language pages to be used in Java Server-side Applications."

This defines a framework to allow scripting language programs to access information developed in the Java platform. We currently plan to integrate this into Mustang for b40. Aside from the framework, we will also include a JavaScript engine based on the Mozilla Rhino implementation. Later, we hope to include a scripting shell that is script language independent. This will be a very cool way to create a prototype, do some exploratory coding, and learn new APIs.

The latest release of Mustang has already included this feature. You can check out the latest release of JavaSE to have a try.

There are 3 blog entries to talk about this:

Example: using JavaScript in Java

Simple JavaScripting from Java

Scripting Support in Mustang - An Example You Can Try

Then why adding scripting languages to Java?

two ways of using Java aware scripting languages: exploratory programming and testing, and user level scripting of applications. - Scripting Languages For Java

What interested me most is that it is a framework but not a simple implementation. So, any other scripting languages (such as PHP, Ruby ... )can be added to Java if someone develop a module implementation following the framework standard. So, it's "An architecture of participation".

//import package
import javax.script.*;

public class Main {
    public static void main(String[] args) {
        try {
            // create a script engine manager
            ScriptEngineManager manager = new ScriptEngineManager();
            // create script engine for JavaScript
            ScriptEngine jsengine = manager.getEngineByName("js");
            // evaluate JavaScript code from String
            jsengine.eval("print('hello world')");
        } catch (ScriptException se) {
            // Handle script exception here..
            // FIXME: do a better job here!
            se.printStackTrace();
        }
    }
}

( 2005年08月09日, 10:05:18 上午 GMT+08:00 ) Permalink 评论 [1]