pluggablejs

pluggablejs is a wrapper around Java's built-in JavaScript engine intended to load pieces of JavaScript code as plugins for programs.
It's a part of some of my Java programs, others can use it although in many cases an ad-hoc solution might be preferable.

Additionally it can be used as a shell to run arbitrary JavaScript code with some extra convenience objects available, although for such uses BeanShell (bsh) makes a lot more sense.
My own, much simpler, RhinoRun can also be used to run arbitrary JavaScript.

Code run in the shell has access a default set of utilities (more details to come).

Running a script

$ java net.outlyer.plugins.Shell file.js

Hello world and pals

helloworld.js
out.println("Hello World!")
$ java -cp pluggablejs.jar net.outlyer.plugins.Shell helloworld.js
Hello World!

namedhello.js
out.println("Hello " + ui.prompt("What's your name?"))
$ java -cp pluggablejs.jar net.outlyer.plugins.Shell namedhello.js
What's your name?: Toni
Hello Toni

hellobye.js
// Includes usage of callbacks and reference to variables defined later on
hooks.atexit(function() {
   out.println('And bye '+you+'!')
});
var you = ui.prompt("What's your name?")
out.printf("Hello %s!\n", new Array(you))
$ java -cp pluggablejs.jar net.outlyer.plugins.Shell hellobye.js
What's your name?: Toni
Hello Toni!
And bye Toni!

plainjava.js
java.lang.System.out.println("Hello world without wrapping");
$ java -cp pluggablejs.jar net.outlyer.plugins.Shell plainjava.js
Hello world without wrapping
  • The first line is evaluated twice, so it shouldn't have side effects.

© 2008 Toni Corvera <outlyer at gmail dot com>

All dates/times in this page are UTC.