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:

importPackage(Packages.net.sf.jasperreports.engine);
importPackage(Packages.net.sf.jasperreports.engine.data);
importPackage(Packages.net.sf.jasperreports.engine.xml);
importPackage(Packages.net.sf.jasperreports.engine["export"]);
…
var jasperDesign = new JRXmlLoader.load(getInputStream(documentURI));
var jasperReport = new JasperCompileManager.compileReport(jasperDesign);
var jasperPrint = new JasperFillManager.fillReport(jasperReport,
           new Packages.java.util.HashMap(), 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”

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s