Lightweight, time-proven approach to Java scripting

While recovering from mild surgery, I stumbled upon a post from Paris Apostolopoulos regarding the fact that the name Javascript is a bit misleading. I know there is a lot of media driven support towards Ruby, Groovy and similar “new” scripting languages, but I still find it natural to use Javascript as my preferred scripting language if I need Java libraries.

For this I use Mozilla Rhino, an open-source implementation of JavaScript written entirely in Java. Basically with Rhino you can import Java classes in your in your scripts and create objects.Here is a piece of Javascript (Rhino) code that uses the JasperReports (java) libraries:

var jasperDesign = new JRXmlLoader.load(getInputStream(documentURI));
var jasperReport = new JasperCompileManager.compileReport(jasperDesign);
var jasperPrint = new JasperFillManager.fillReport(jasperReport,
           new, dbConn);
new JasperExportManager.exportReportToHtmlFile(jasperPrint, “myreport.html");

There are a lot of things to consider when choosing a scripting language and I’m sure that there will be people saying that Ruby is better in this way or Groovy in another way but Javascript/Rhino feels natural and has the minimal learning curve due to the similar syntax.

BTW the Rhino engine is an integral part of Apache Cocoon that supports continuations.

UPDATE: Read Stevey’s blog on “Rhino on Rails”


2 thoughts on “Lightweight, time-proven approach to Java scripting

  1. Perhaps Groovy would present an even smaller learning curve, if we only consider the syntax. But of course (too) many people are already acquainted to some extent with JavaScript, so that would make it a winner most of the time.

    And for the more adventurous souls there is always Scala 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s