Installing Rhino on OS X
Rhino is Mozilla's JavaScript implementation in Java.
Installing Rhino on OS X is a piece of cake.
Get the Rhino archive from Mozilla.
$ mkdir ~/Desktop/src/
$ cd ~/Desktop/src/
$ curl -O ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_6R5.zip
Double click that downloaded zip file and StuffIt expands it.
I am not a Java developer so don't just happen to have a certain special directory on my Mac ~/Library/Java/Extensions/. Java on Mac must look into this directory and load whatever is there. I'm not sure exactly how it works but there is no need to play with the CLASSPATH environment variable if we use this Extensions directory. (where I learned about this directory)
Make the directory and move the Rhino jar file there.
$ mkdir ~/Library/Java/
$ mkdir ~/Library/Java/Extensions/
$ cp ~/Desktop/src/rhino1_6R5/js.jar ~/Library/Java/Extensions/
Try the Rhino shell
$ java org.mozilla.javascript.tools.shell.Main
js> help();
js> java.io.file
[JavaPackage java.io.file]
js> quit();
Note how the whole java package is available to JavaScript running in Rhino. That means, for example, we don't have to write file manipulation libraries in C like is necessary with Spidermonkey. All of Java is avaliable! The most we have to do is wrap the Java API in something a little more JavaScript-ish. The Helma source code gives examples of this type of wrapping.
We can also run a little JavaScript file with Rhino. First create the file
$ mate ~/Desktop/hello.js
with only one line.
print('hello, world');
Run the script in the file.
$ java org.mozilla.javascript.tools.shell.Main ~/Desktop/hello.js
hello, world
Make starting the Rhino shell a little easier. (Note that this alias will compete with your Spidermonkey binary also called js if you have that binary in your PATH.)
$ alias js="java org.mozilla.javascript.tools.shell.Main"
$ js
js> quit();
$ js ~/Desktop/hello.js
hello, world
Some more info about Rhino shell scripting.
After installing Spidermonkey and installing Rhino I can now clearly see why most server-side JavaScript projects are using Rhino. Starting with Spidermonkey is like starting from scratch. I'm still trying to compile Spidermonkey with NSPR just to have a File object and that still leaves all of the rest of what is in the java for me to write. Starting with Rhino is starting with the whole Java API. Maybe Spidermonkey is faster but given what Rhino provides makes using Spidermonkey seem like premature optimization for someone trying to create a whole server-side framework.
Comments
Have something to write? Comment on this article.
As of March 3rd, 2009 the latest major release for Mozilla's Rhino is 1.7R2.
FTP address for this file -- ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_7R2.zip
Have something to write? Comment on this article.
feed
If you want to invoke a server side JavaScript program from a cgi call, you'll need to put the js.jar file into your cgi-bin folder.
Here's a short script that will work: