Can you convert Java Applications to HTML5 and WebAssembly/JavaScript?

 13.08.2020 -  Ryan Forde

Despite popular belief, Java is far from dead and is actually the second most popular programming language overall, particularly in Enterprise Software.

It still enjoys a very active community, a rich ecosystem of tooling, a ton of libraries and frameworks, it is able to adapt and evolve while being backwards compatible (mostly) and it is still widely used in the industry.

Modern Enterprise Applications

While Java provides a great environment for server-side and native applications, modern enterprise application clients are increasingly moving toward HTML5 standards. This is because they leverage broader compatibility across a wide range of browsers, devices, and environments to satisfy user demand.

HTML5 applications leverage standards such as HTML, CSS, and JavaScript, which have notoriously finicky implementations in different environments, browsers, and versions. In addition to learning the new languages and numerous frameworks of HTML5 frontend development, Java developers need to wrap their heads around new libraries, event models, and the prototype-oriented OOP paradigm used by JavaScript.

WebAssembly and modern browsers change this completely. By allowing to look at the browser as a compilation target, WebAsembly eliminates these tradeoffs.

It allows your Java developers to stick to their guns and avoid learning the quirks of JavaScript. You can work with what you already know, and more importantly what your team already knows. You have one less language to learn and you get to keep strong typing!

What is WebAssembly?

WebAssembly, or WASM for short, is a small, fast binary format that promises near-native performance for web applications. It is a standard supported by over 93% of the browser stock (July 2020).

WebAssembly is not here to replace JavaScript applications. Instead think of WASM as the companion to JavaScript. JavaScript is flexible, dynamic and delivered through human-readable source code, WebAssembly is fast, strongly typed and delivered via a compact binary format.

WebAssembly combines with HTML5 and JavaScript to allow high-performance applications to run in browser. Being widely supported on any modern browser, it is the ideal target for a Java conversion tool.

How to convert Java to HTML5

There are currently a few options when it comes to compiling Java to HTML5 :

  • TeaVM - an ahead-of-time compiler of Java bytecode, that’s capable of emitting JavaScript and WebAssembly, with a limited runtime environment. Excellent for writing new Java applications targeting the browser.
  • JWebAssembly - A Java bytecode to WebAssembly compiler. It can generate the WebAssembly binary or text format. It is written in Java itself and can be integrated with other Java build tools. It has a limited runtime (excluding Swing, AWT) and is good for writing new applications from scratch.
  • CheerpJ - combines an AOT Java bytecode compiler with a full Java runtime in WebAssembly and JavaScript, allowing virtually any Java application to be converted to JavaScript without manual intervention. Compatible with Swing, AWT, Spring and any other Java framework, it is meant to convert existing Java applications to HTML5.

Why CheerpJ?

Differently from alternative solutions, CheerpJ does not require any change to be made to the source code of the application, supports 100% of the Java language and runtime, and is not a framework and thus in no way opinionated.

CheerpJ’s primary target is to convert and migrate existing Java client applications to HTML5, including Swing clients, Java Applets, and Java libraries.

Using CheerpJ to convert your Java application is straightforward:

  • Convert ahead-of-time all your .jar archives using the CheerpJ compiler;
  • Host the resulting JavaScript and WebAssembly on your web server;
  • Embed your converted application (or link, if a library) in a HTML page, and access it from any browser.

CheerpJ does not require any server-side support, as all application components (converted application and runtime) are static. You can put a CheerpJ application behind a CDN to virtually cancel any bandwidth costs.

CheerpJ is available for Windows, macOS and Linux, and can be downloaded here .