Sunday, 2 August 2015

HTML5 Browser Apps with Java - TeaVM part 3

Continuing my trials with TeaVM, after establishing how to do some common tasks, it was time to try it out on a more substantial project.

As well as offering a developer-friendly way to create shiny new browser apps using Java, TeaVM also provides an opportunity for old client-side Java apps to be updated into the modern age. I turned to some of my existing Java software that would benefit from being ported into modern, HTML5-compatible browser applications. A perfect example is old code that was originally a Java Applet - from over 15 years ago! Of course, the user interfaces for web and client-side Java are significantly different, so these parts would need to be re-written. But the task would hopefully be made easier by the fact the much of the core game logic could remain intact.

Some time ago I wrote about how existing client-side Java code could be ported to Android. TeaVM offers the possibility of something similar by making it easy to port Java apps to the browser. In fact, with Java being the common starting point, it's possible to have an app on multiple different platforms - native Java, Android, and Web, using the same core code, with platform-dependent differences only for the UI elements.

Here is such an example - a simple arcade style game called Super Bunny. Originally written as a Java applet, I ported it to Android in around 2010. So the question is, would it be possible using TeaVM to now port it to HTML5/Javascript? The answer turned out to be a definite Yes, and here it the proof:
Play the Web version created using TeaVM

The game logic code was almost entirely re-usable, and is shared by the original applet, the Android version, and now the Web version too. Although re-writing the GUI was tricky in places, many of these problems stem from limitations in Javascript itself, such as lack of good multithreading or inbuilt resource management. It's fair to say, I did come across a number of bugs or missing features in TeaVM itself, but that's to be expected for a project still in its early stages. Having reported these back to the TeaVM developers, I found they were quickly fixed or workarounds suggested, and so progress with my trial was nevertheless rapid.

All in all I am satisfied with the results of my experiments with TeaVM. I recommend it to the Java developers out there that would like to do client-side web development but have an aversion to writing native Javascript. It has the potential to displace older, competing technologies like GWT, and I look forward to seeing how the project progresses. Nice work TeaVM Team!


  1. > many of these problems stem from limitations in Javascript itself, such as lack of good multithreading

    Lack of multithreading in JavaScript is not a problem for TeaVM anymore. It is capable of producing continuations, so there is thread emulation. You can try new Thread().start() in TeaVM and get desired result. Look at this example:

  2. Ah, I hadn't noticed support for Thread had been added, thanks for pointing it out!