The JavaScript blog.


java runtimes vert.x nashorn reactive oracle

Vert.x 3.0

Posted on .

Paulo Lopes wrote in with the news that Vert.x 3.0 is out (License: Eclipse/Apache 2.0). Vert.x is a JVM-based toolkit for building applications that supports multiple languages, including JavaScript. It's event driven and non-blocking, so it originally received interest from Node programmers, but it currently seems more popular in the Java community.

Here's an example of a Vert.x web server:

  .requestHandler(function (req) {
      .putHeader('content-type', 'text/plain')
      .end('Hello from Vert.x!');

Version 3 has a new API designed specifically for building web applications. It's called Vert.x-Web, and it supports routing, body and cookie parsing, authentication, static files, and it has an event bus bridge.

The routing API looks like many other server-side JavaScript web frameworks:

var route = router.route('POST', '/catalogue/products/:productype/:productid/');

route.handler(function(routingContext) {  
  var productType = routingContext.request().getParam('producttype');
  var productID = routingContext.request().getParam('productid');

  // Do something with them...

Vert.x 3 has support for MongoDB, Redis, and JDBC. It also supports reactive streams, and it has RxJava style APIs if you want to avoid using too many nested callbacks. The reactive API supports things like converting readable streams to observables (Rx.Observable.fromReadStream), and asynchronous future objects (Rx.observableFuture). Vert.x pushes reactive programming pretty hard, so if reactive programming is your thing then you might want to try it out.

Vert.x's JavaScript engine is Oracle's Nashorn. As far as I know, most of the ES6 features that you'd actually want (like classes) are not yet supported. Nashorn gets updated when the JDK gets updated, so it might be a while before several key ES6 features are ready. I couldn't get Babel working with it, but it seems likely that it could be adapted to work. Someone in the Vert.x community may have already ported an ES6 transpiler.

If you want to read more about Vert.x 3 and Nashorn, I found a recent interview with Tim Fox, the creator of Vert.x, and there's an official Nashorn blog.


java node

Nodyn: No Dice

Posted on .

Nodyn (GitHub: projectodd / nodyn, License: Apache 2.0) is a Node API-compatible JVM-based project. That means you can technically use Java libraries from within Node programs.

I've been using it on my Mac, running Mavericks. Here's what I had to do to get it to work:

brew install maven  
git clone https://github.com/projectodd/nodyn.git  
cd nodyn  
export JAVA_HOME=`/usr/libexec/java_home`  
mvn install -Dmaven.test.skip=true  
cd nodyn-standalone/target  
java -jar nodyn-standalone.jar --console  

It took me a while to figure all of this out. I already had Homebrew installed, but I didn't have Maven. I'm an amateur Android developer, so I only ever really write Java through Google's recommended IDE tools.

Maven installed without too much trouble, except I found it used the wrong version of Java. The export JAVA_HOME line makes Maven use the right version. I'm not sure why this is required because java -version showed 1.7, but for some reason Maven was building Nodyn with 1.6, which generated a long and inscrutable error message.

The mvn install -Dmaven.test.skip=true line builds Nodyn, and skips tests. I wanted to skip the tests because they seemed to hang on this line:

Starting test: src/test/resources/os|os_test.js|testFreemem  

Once I built it, I ran a small program that reads its own source and prints it to stdout:

var fs = require('fs');

console.log('I can print my own code');

fs.readFile('test.js', 'utf8', function(err, text) {  
  if (err) console.error(err);
  console.log('When I work correctly');

This printed the following output, which is incorrect:

log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).  
log4j:WARN Please initialize the log4j system properly.  
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.  
I can print my own code  

The expected output is this:

I can print my own code  
var fs = require('fs');

console.log('I can print my own code');

fs.readFile('test.js', 'utf8', function(err, text) {  
  if (err) console.error(err);
  console.log('When I work correctly');

When I work correctly  

It seems like Node API compatibility isn't quite there yet. I also noticed it takes much longer than Node to start up, but I seem to remember jRuby developers complaining about startup time so that might be something to do with how Java works. It probably doesn't really matter for long-running server processes, but I quite like the fact Node programs start up quickly.

If you're a Java programmer Nodyn might seem cool, but so far I've struggled with it. Despite my Maven issues, the project looks neatly organised and carefully written, so I'm going to keep watching it.


java testing


Posted on .

JsTestDriver is a test runner that you can use to run tests against different browsers.
Browsers are treated like sandboxes, using a capturing API that allows
tests to run against multiple browsers. It works on the command line, so
once a browser has been captured it can be minimized and forgotten

It can run tests in parallel as well, so running tests against many
browsers on many platforms isn't any more time consuming than a single
test run. The production and test code is stored in each browser and
reloads only when source files have changed, so this adds an additional
speed boost.

This video by the author explains the basics:

Tests look like this:

GreeterTest = TestCase("GreeterTest");

GreeterTest.prototype.testGreet = function() {
  var greeter = new myapp.Greeter();
  assertEquals("Hello World!", greeter.greet("World"));

If you'd like to try it out, the introductory

should get you up and running pretty quickly.


java packaging


Posted on .

JavaGems is a JVM packaging system based on Gemcutter. It's a user-friendly way of distributing and building Java libraries and binaries. It uses
RubyGems and works the same way.

One reason this is useful to JavaScript developers is because the
JavaGems authors have added a Rhino package. This was automatically
imported from maven build scripts, which means it won't currently run
just by typing js.

However, if you'd like to try it anyway, follow these steps:

  1. Update or install RubyGems
  2. To update RubyGems, type gem update --system
  3. Install JavaGems: gem install javagems
  4. Install Rhino: javagem install js

You can run Rhino with jam:

echo "gem 'js'" > Gemfile
jam org.mozilla.javascript.tools.shell.Main

One of the JavaGems authors said the package will be modified to run out of the box in the near future.

The source for the JavaGems
is on GitHub. This
includes the following tools:

  • javagem - install and manage gems
  • jam/jamc - unifies running Java executables