Inhaltsverzeichnis

Standalone Javascript

This page is about Rhino, the scripting engine that comes with Java 1.6.

Basic Usage

jrunscript scriptfile.js arg1 arg2 ...
Using a shebang like !#/usr/lib/java/bin/jrunscript doesn't work so far!

jrunscript -q reveals the version of Rhino.

See also the manpage

Built-in global Properties and Functions

Did not find a complete document yet. The jrunscript JavaScript built-ins (alternaltive) are not complete (e.g. print() / println() are missing). The functions listed in Rhino Shell are obviously not in sync with jrunsricpt (e.g. readFile() is not available).

The IMHO only way to really find out which functions you have is to have a script print out the functions. This is described here: Global Properties and Functions. Here is a shortcut:

jrunscript -e 'for (i in this) echo("/** "+i+" */", this[i])'

Interaction with the underlying OS

Accessing Command Line Arguments

Use the predefined arguments array:

for (var i=0; i<arguments.length; i++) println('Arg '+ i+ ' '+ arguments[i]);

Return exit code

exit(code)

Writing to stdout

echo('Hello world!');
println('Hello World!');
print('Hello World!\n');
printf('Hello World!\n');

Writing to stderr

Did not found a built in function, so i use the java library:

java.lang.System.err.println("Error")

Reading from stdin

FIXME use read

Supporting CLI and HTML Frontend

From within a Webbrowser, you have access to the DOM API whereas from jrunscript (with Rhino) you have access to the jrunscript Built-ins and the Java-API. The only intersection are the javascript built in core functions and objects.

In order to support both a CLI- and a Web frontend one should separate the frontend-specific code from the generic application code:

cli.js      <---.
                |--- core_app.js 
html_dom.js <---´
load('core_app.js')
core_app() // function is defined in core_app.js