JavaScript for Cheminformatics - Cross-Compiling Java to JavaScript with GWT Revisited

January 06, 2009

Wouldn't it be great to be able to cross-compile your Java cheminformatics source code to plain old JavaScript files to be used in your Web applications without plugins? JavaScript is the most widely-deployed runtime environment in existence; on today's Web it's one of the few things you can count on being there. Java is one of the most advanced programming languages in existence, with a mountain of developer tools and libraries.

All of which makes the development of software in Java and its deployment via JavaScript an attractive option. Until recently, very few examples of using JavaScript for low-level cheminformatics functionality were available. This is starting to change.

Demonstrations

Last month, Duan Lian showed that cross-compiling Java cheminformatics libraries to JavaScript is indeed now possible. His two proofs of concept give hints as to what may soon become routine:

  • MX-GWT An online demo shows that molecular weight can be calculated from a molfile without any plugins.
  • Demo Molecule Structure Rendering This remarkable online demo uses a pure-JavaScript cheminformatics toolkit to render color 2D chemical structures from molfiles independent of a Java runtime.

Both of these demos make use of MX, the lightweight, platform-independent, cross-language (link, link) cheminformatics toolkit. Using the Google Web Toolkit (GWT), Duan compiled MX into pure JavaScript that can be run in a browser without any plugins.

Still More Demonstrations

Recently, Noel O'Boyle upped the ante by demonstrating a spinning 3D molecular model created in pure JavaScript. His demonstration doesn't yet parse a molfile but rather manually creates the graphical representation.

In a comment on Noel's work, Richard Hall announced a simple 2D molecular renderer in pure JavaScript.

What all of these demonstrations could benefit from is a robust, low-level, pure-JavaScript cheminformatics toolkit.

The Problem: Simple Java to JavaScript Cross-Compilation

When used in its most popular mode, GWT isn't just a Java to JavaScript cross-compiler; it's a full-blown method for developing Web sites complete with GUI APIs, HTML and JavaScript output, and a complete project structure.

In other words, GWT is overkill for the job at hand, which is simply to convert a Java library consisting of a set of source files into one or more JavaScript files.

I've been trying to understand how to access GWT's low-level JavaScript cross-compiler functionality without the rest of the GWT, with mixed success. The good news is this appears to be one of the ways the creators of GWT envisioned it to be used; the bad news is I've found no documentation describing exactly how to do it.

Surprisingly, getting this seemingly obvious use case to work is anything but obvious. Most documentation dives right into using GWT to build fancy websites. What's needed here is far more basic.

What would be most helpful would be a step-by-step procedure for compiling a simple HelloWorld.java class into a HelloWorld.js JavaScript file, preferably using only command-line tools:

public class HelloWorld
{
  public String sayHello()
  {
    return "hello";
  }
}

The result could then be used in client-side JavaScript by including the HelloWorld.js file and invoking the HelloWorld.sayHello method.

If you have any experience in this area, I'd be very interested to hear about it. If no guides to solving this simple problem exist, then the first may appear here!

Conclusions

A reliable method for cross-compiling Java libraries into JavaScript libraries with GWT would open many new avenues for cheminformatics Web applications. Future articles may detail how this can be accomplished.

Image Credit: Dmitry Baronovskiy