Rhino Javascript Engine

A friend of mine thinks he can beat my score on the gild.com triangle numbers challenge. His only excuse is getting rhino setup on his MacBook to test the JS. So here is a quick and dirty approach.

  • Grab the Rhino source.
  • Un-zip/tar to a cool directory. I use: ‘/Users/username/Development/Tools/Rhino’
  • Edit your .bash_profile file:
export PATH=$PATH:/Users/username/Development/Tools/Rhino
alias rhino='java -jar /Users/username/Development/Tools/Rhino/js.jar'
  • Add a print statement.
print(fibs(12));
  • Run from the console using:
rhino fibs.js

Questions, Comment?

UPDATE

I found an issue when trying to test a js file. Because ‘rhino’ is an alias bash will not run it as a command. I found a better solution here: WorkingRhino. Check it out, or just follow my instructions below:

  • Remove the rhino alias from .bash_profile or .profile.
  • Create a shell script in ‘/Users/username/Development/Tools/Rhino’ named rhino
  • Copy/Paste this:
#!/bin/bash
dir=$(dirname $0)
export CLASSPATH=${CLASSPATH}${CLASSPATH:+:}${dir}/*
echo using CLASSPATH=$(printenv CLASSPATH) > /dev/stderr
[ "$1" = "-f" ] || { RLWRAP="rlwrap -C js" ;}
exec $RLWRAP java ${JAVAOPTS} \
org.mozilla.javascript.tools.shell.Main -strict "$@"
  • Make rhino executable with chmod u+x rhino
comments powered by Disqus