Installing Rhino on Mac

February 20th, 2009. Tagged: JavaScript, mac, tools

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!

Tell your friends about this post: Facebook, Twitter, Google+

17 Responses

  1. [...] performance, php, phpbb, tools, yslow, yui, writing, music,… life and everything. « Installing Rhino on Mac Search site bookmarklet with YUI and BOSS [...]

  2. 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

  3. [...] is server side Javascript, built in Java, that can run on OS X and other unix-like platforms. window.onload = [...]

  4. 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>

  5. 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

    ;)

  6. 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

  7. 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

  8. You made some good dots thither. I looked for this issue and found out. that most people will agree with your blog….

  9. 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

  10. 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

  11. 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!

  12. unzip…

    [...]Installing Rhino on Mac / Stoyan’s phpied.com[...]…

  13. Is this installation method useable with other Java Libraries? Example being lwjgl or Jbullet? also thanks!

  14. [...] Installing Rhino on Mac [...]

  15. [...] Installing Rhino on Mac [...]

  16. @Cody Soyland

    Alternatively, you can install rhino using mac ports and the command is automatically:

    $ rhino
    js >

    or

    $ rhino script.js
    js >

  17. This really helped!!! Thank you so much!

Leave a Reply