Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW