To quote http://www.mozilla.org/rhino/:
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically embedded into Java applications to provide scripting to end users.
Rhino allows you to use JavaScript:
- on the server-side, so you can ditch RoR, Perl, PH... well, keep PHP 🙂 ... in favor of JavaScript
- on the command line, so you can shell scripts
Let's see how you can install Rhino on OSX.
Step 1 - download and unzip
Download the binary from the Rhino site and unzip to a temporary directory, say /tmp
. On the command-line:
$ curl ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_7R1.zip > /tmp/rhino.zip $ cd /tmp $ unzip rhino.zip
Now you have the file /tmp/rhino1_7R1/js.jar
Step 2: move js.jar where Java can find it
Your default Java install (comes "free" with OSX) will look for class libraries in a predefined directory ~/Library/Java/Extensions
. This directory may not exists, so create it and move the js.jar there.
$ mkdir ~/Library/Java $ mkdir ~/Library/Java/Extensions $ mv /tmp/rhino1_7R1/js.jar ~/Library/Java/Extensions/
Step 3: Done! Now test it
That's all there is, your Rhino install is ready to use. To launch and test the Rhino shell try:
$ java org.mozilla.javascript.tools.shell.Main Rhino 1.7 release 1 2008 03 06 js> print('hello!') hello! js> parseInt('123abc') 123 js> encodeURI('hola LA!') hola%20LA! js> for (var i = 0; i < 5; i++) > print('i is now ' + i) i is now 0 i is now 1 i is now 2 i is now 3 i is now 4 js> quit()
Last example - create a script that reads the HTML source of my blog:
$ echo "print(readUrl('http://phpied.com'))" > read.js
now you have a script called read.js, let's run it:
$ java org.mozilla.javascript.tools.shell.Main read.js
Thanks for reading!
And happy JS-scripting!
Comments? Feedback? Find me on Twitter, Mastodon, Bluesky, LinkedIn, Threads