====== Standalone Javascript ==
This page is about [[http://www.mozilla.org/rhino/|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 [[http://download.oracle.com/javase/6/docs/technotes/tools/share/jrunscript.html|manpage]]
===== Built-in global Properties and Functions ==
Did not find a complete document yet. The [[http://www.silab.dsi.unimi.it/manual/JDK%206%20Documentation/technotes/tools/share/jsdocs/index.html|jrunscript JavaScript built-ins]] ([[http://download.oracle.com/javase/7/docs/technotes/tools/share/jsdocs/GLOBALS.html|alternaltive]]) are not complete (e.g. print() / println() are missing). The functions listed in [[https://developer.mozilla.org/en/Rhino_Shell|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: [[http://www.herongyang.com/JavaScript/Built-In-Object-Global-Properties-and-Functions-jrunscript.html|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
==== 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 [[https://developer.mozilla.org/en/JavaScript/Guide/Functions#Predefined_Functions|functions]] and [[https://developer.mozilla.org/en/JavaScript/Guide/Predefined_Core_Objects|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