Installing Rhino on Mac
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!
This entry was posted on Friday, February 20th, 2009 and is filed under JavaScript, mac, tools. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

February 23rd, 2009 at 3:55 am
[...] performance, php, phpbb, tools, yslow, yui, writing, music,… life and everything. « Installing Rhino on Mac Search site bookmarklet with YUI and BOSS [...]
June 22nd, 2009 at 9:46 am
Stoyan,
Thanks for the helpful post. Got rhino running on my mac.
You might want to update the link to rhino zip: the latest version is 7R2 now. Also, you might want to use the “-p” option in mkdir :
mkdir -p ~/Library/Java/Extensions
This will create the parent directories if they don’t exist.
-Vipul
August 8th, 2009 at 11:21 am
[...] is server side Javascript, built in Java, that can run on OS X and other unix-like platforms. window.onload = [...]
September 17th, 2009 at 12:49 am
Thanks for this article. Worked fine for me. Also worth pointing out is that you can alias the word “rhino” to the java command. Add this line to your ~/.profile:
alias rhino=”java org.mozilla.javascript.tools.shell.Main”
Now you have a simpler command to use:
# rhino << hi
js>
March 12th, 2010 at 2:54 pm
Hi Stoyan!
Thank you for the clear outline of Rhino install steps. BTW:
$ mkdir ~/Library/Java
$ mkdir ~/Library/Java/Extensions
can be compressed to:
$ mkdir -p ~/Library/Java/Extensions
March 25th, 2010 at 3:48 pm
You can create JavaScript files that will run with out naming the interpreter first.
Just make the script executable and make the first line:
#!/usr/bin/env java org.mozilla.javascript.tools.shell.Main
So read.js would contain:
#!/usr/bin/env java org.mozilla.javascript.tools.shell.Main
print(readUrl(‘http://phpied.com’))
Then you can run it by just typing read.js:
$ ./read.js
May 14th, 2010 at 9:10 am
Hi Stoyan,
thank you for the post. I did not know that I can put the Java extension to ~/Library/Java/Extensions – cool: got Rhino up and running.
Michael
January 8th, 2011 at 3:27 am
…
You made some good dots thither. I looked for this issue and found out. that most people will agree with your blog….
August 28th, 2011 at 12:42 pm
No, do not put any Jars in the default locations and pollute the system namespace. Always, always, always, set the class path from the command line — java -cp rhino.jar org.mozilla.javascript.tools.shell.Main
September 16th, 2011 at 9:21 am
Hi Stoyan,
Got rhino running on my mac.
if the parent directories don’t exist., update the link to rhino zip: the latest version is 7R2 now. Also, you might want to use the “-p” option in mkdir :
mkdir -p ~/Library/Java/Extensions
Thanks for the helpful post.
ekosukmo-checko
September 16th, 2011 at 9:23 am
Hi Stoyan,
You can enable up-arrow command completion in Rhino using JLine:
java -classpath js.jar:jline.jar jline.ConsoleRunner org.mozilla.javascript.tools.shell.Main
Go get it at http://jline.sourceforge.net/. You’ll need to put it in your classpath; I throw rhino and jline into ~/Library/Java/Extensions/, which gets added to the CLASSPATH automatically for me.
Also, ctrl-D exits the Rhino shell too!
January 23rd, 2012 at 6:30 am
unzip…
[...]Installing Rhino on Mac / Stoyan’s phpied.com[...]…
November 15th, 2012 at 11:59 am
Is this installation method useable with other Java Libraries? Example being lwjgl or Jbullet? also thanks!
January 18th, 2013 at 6:21 am
[...] Installing Rhino on Mac [...]
January 18th, 2013 at 12:07 pm
[...] Installing Rhino on Mac [...]