1. Overview

In the fast-paced world of web development, the introduction of WASM (WebAssembly) has presented developers with new possibilities. It allows them to leverage the speed and adaptability of compiled languages on the web platform.

In this tutorial, we’ll explore the process of compiling Java to WebAssembly and investigate the available tools and methodologies.

2. What is WASM (WebAssembly)

WebAssembly is a low-level binary instruction format that can be run in modern web browsers. It allows developers to run code written in languages like C, C++, and others on web browsers at near-native speeds. WebAssembly is designed to run alongside JavaScript, allowing both to work together.

WebAssembly isn’t intended to be written by hand. Instead, it’s designed as an effective compilation target for source languages like C, C++, and Rust. We can import WebAssembly modules into a web (or Node.js) application, thus exposing its functions for utilization through JavaScript.

We need a specialized compiler to convert the source code into the WASM format for using native languages with WebAssembly. To execute the format in the browser, we must load and initialize the binary file using JavaScript. The below figure illustrates the path from native code to a WASM file:

How to Use WASM in browser

JS serves as the central interface between WASM, HTML, and CSS, as WASM currently lacks direct access to a web page’s Document Object Model (DOM). WASM provides imports and exports for interaction with JS. The exports consist of functions from the source code compiled to WASM, which JS can access and execute similarly to JS functions. The imports allow JS functions to be referenced within WASM.

3. Different Tools for Compiling Java to WASM

Java, one of the most popular programming languages, has also found its way into this ecosystem through various tools and frameworks. Now, we’ll look into different prominent tools for converting Java code into WebAssembly:

3.1. TeaVM

TeaVM is an ahead-of-time compiler for Java bytecode that emits JavaScript and WebAssembly that runs in a browser. The source code isn’t required to be Java, so TeaVM supports any JVM language, including Kotlin and Scala. TeaVM produces smaller JavaScript that performs better in the browser.

TeaVM optimizer can eliminate dead code and produce very small JavaScript. It reconstructs the original structure of a method, resulting in JavaScript that is almost similar to what we manually write. It also supports threads and is very fast.

3.2. JWebAssembly

JWebAssembly specializes in compiling Java bytecode to WebAssembly code. It can compile any language that compiles to Java bytecode like Groovy, Kotlin, and Scala. JWebAssembly leverages the LLVM toolchain to generate optimized WebAssembly output.

It also supports features such as native methods, exception handling, and garbage collection. JWebAssembly optimizer fine-tunes the WebAssembly output of individual methods post-transpilation. It ensures optimal performance before finalizing the output.

3.3. CheerpJ

CheerpJ is a WebAssembly-based JVM for the browser. It can execute Java applications from the browser without Java installation. CheerpJ can run any Java application, applet, and library on modern browsers.

CheerpJ supports 100% of the Java 8 SE Runtime, as well as native reflection and dynamic class creation. It also supports file access, networking, clipboard, audio, and printing. It is also compatible with Java Swing, Oracle Forms, EBS, and other third-party frameworks.

4. Conclusion

In this article, we understood WASM and looked at an overview of the tools used to convert Java code into WebAssembly.

TeaVM is excellent for writing new Java applications targeting the browser, whereas JWebAssembly has a limited runtime and is good for writing new applications from scratch. CheerpJ doesn’t require any change to the application’s source code; it’s meant to convert existing Java applications to HTML5.

The choice of Java as a WASM tool depends on project requirements, performance considerations, and developer preferences. By understanding the features and trade-offs of each tool, we can decide on the appropriate framework.