Journaling on estrip is free and easy. get started today

Last Visit 2010-06-17 00:44:29 |Start Date 2007-01-28 02:45:40 |Entries 2 |Images 1 |Theme |

Category: javascript

01/28/07 03:08 - ID#37895

Working WIth Rhino Javascript Engine

Welcome to Paul's new space for fun programming tips and ideas. I decided to separate this from my personal journal even though I do not let other users have multiple journals.

I know the outside links don't work right this minute. I fix it tomorrow. In the mean time I am having fun with Rhino , a command line java based javascript interpreter than lets you run javascript as a shell scripting language which harnesses the power of java.

It is over at the mozilla foundation site. It is great being able to read and write files, process data and run system commands with jjavascript. If you are interested in that sort of thing you should download it and play around with it. It works on linux, mac os x, and windows.

After downloading the last version of rhino zip and extracting the contents, you can run any javascript file at the command prompt by typing this in the rhino directory you download. You can move the js.jar somewhere else and put it in your path if you want to be able to use it from else where

java -jar /path/to/js.jar myFile.js myArgument1 myArgument2

The first part of the statement tells the computer you are running a java program in jar format the next argument is the name of the jar file which is js.jar, the executable that comes with rhino and interprets javascript. The next argument is the name of the javascript file you want to run. IN this case myScript.js and the last set of data are any arguments you want to pass to the script. Arguments are read from the script just like they are in any javascript function, by referencing the arguments array.

So lets make a simple file.
sayMyName.js
[box]
if(arguments[0]){
print('Hello '+arguments[0]);
} else {
print('Give me your name as the first argument of the script');
}
[/box]
Then run java -jar js.jar sayMyName.js Paul

This would print Hello Paul. If you left off the Paul argument it would say Give me your name as the first argument of the script'. The print command prints to the terminal window.

Here is some great ifo about setting the javascript interpreter up in you bash shell so that you can simply type js myJsFIle.ks to run the javascript from anywhere. There is also information about running system commands.

print addComment

Permalink: Working_WIth_Rhino_Javascript_Engine.html
Words: 428


Date Cloud

Category Cloud

  1. User must have at least 3 blogs in one category for categories list to show.

More Entries

My Fav Posts

  1. This user has zero favorite blogs selected ;(

Search

Chatter

New Site Wide Comments

joe said to joe
Never send a man to do a grandma's job...

sina said to sina
yes thank you!
Well, since 2018 I am living in France, I have finished my second master of science,...

paul said to sina
Nice to hear from you!! Hope everything is going great....

paul said to twisted
Hello from the east coast! It took me so long to see this, it might as well have arrived in a lette...