Execute system command with the Rhino JavaScript engine
This small, elegant function will execute the given command arguments when using the Rhino JavaScript engine, made possible via Java. This implementation was used to provide JS Growl the ability to execute the growlnotify binary.
/**
* Execute the given _cmd_, returning an array of lines from stdout.
*
* Examples:
*
* exec('growlnotify', '-m', msg)
*
* @param {string ...} cmd
* @return {array}
* @api public
*/
function exec(cmd) {
var lines = [], line
with (JavaImporter(java.lang, java.io)) {
var proccess = Runtime.getRuntime().exec(Array.prototype.slice.call(arguments))
var stream = new DataInputStream(proccess.getInputStream())
while (line = stream.readLine())
lines.push(line + '')
stream.close()
}
return lines
}Bookmark/Search this post with:
Delicious
Digg
StumbleUpon
Reddit
Facebook