Rhino (JavaScript engine)

From Wikipedia, the free encyclopedia

Jump to: navigation, search
Rhino
Developed by Mozilla Foundation / Mozilla Corporation
Latest release 1.7R2 / 2009-03-22; 20 days ago
Written in Java
Operating system Cross-platform
Platform Java Virtual Machine
Type JavaScript engine
License MPL 1.1/GPL 2.0
Website http://www.mozilla.org/rhino/

Rhino is an open source JavaScript engine. It is developed entirely in Java and managed by the Mozilla Foundation. The Foundation also provides an implementation of JavaScript in C known as SpiderMonkey.

Rhino converts JavaScript scripts into Java classes. Rhino works in both compiled as well as interpreted mode. It is intended to be used in server-side applications, hence there is no built-in support for the browser objects that are commonly associated with JavaScript.

Rhino can be used as a debugger by making use of the Rhino shell. It can be used in applications by embedding Rhino.

A slightly modified version of Rhino 1.6r2 comes bundled with the Sun release of Java SE version 6, which was released in December 2006. This makes it easier to integrate JavaScript as part of Java programs and to access Java resources from JavaScript. Other implementations of Java 6 may differ: for instance, the version of Java 6 supplied by Apple for Mac OS X does not include a JavaScript engine. [1]

Contents

[edit] History

The Rhino project was started at Netscape in 1997 and released to the Mozilla Foundation in 1998. It was made open source thereafter[2]. The project gets its name from the animal on the cover of the JavaScript book from O'Reilly Media [3].

The last 1.7R1 version relies on the Java 5 platform, and supports version 1.7 of JavaScript[4].

[edit] Example

Below is an example of java code running JavaScript print('Hello, world!')

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
 
public class RhinoEngine {
    public static void main(String[] args) {
        ScriptEngineManager mgr = new ScriptEngineManager();
        // Now we can go and get a script engine we want. 
        // This can be done either by finding a factory that supports 
        // our required scripting language 
        // (engine = factory.getScriptEngine();)
        // or by requesting a script engine that supports a 
        // given language by name from the script engine manager.
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
 
        // Now we have a script engine instance that 
        // can execute some JavaScript
        try {
            engine.eval("print('Hello, world!')");
        } catch (ScriptException ex) {
            ex.printStackTrace();
        }    
    }
}

Here is a simple example showing a Rhino Script that outputs each line in the stdin converted to uppercase.

// The same as: import java.io.*;
importPackage(java.io);
importPackage(java.lang);
 
// "in" is a keyword on Javascript. 
// In Rhino you could query for an attribute using [] syntax: 
// Ex: System['out']
S = new BufferedReader( new InputStreamReader(System['in']) );
s = true;
 
while (s){
    s = S.readLine().toUpperCase();
    if (s) System.out.println(s);
}

[edit] See also

[edit] References

[edit] External links

Personal tools