Java in WebAssembly
Many exciting WebAssembly things are happening in the Java ecosystem. There are a few projects that are rapidly maturing, and that can generate browser-oriented JS.
The various projects handle memory management differently. Their feature sets also differ.
Uses
All of the existing Java implementations are browser oriented, and are designed to let developers write Java and link to it from JavaScript.
Available Implementations
There is a working Wasm plus WASI version of TeaVM that allows you to run Java apps in Spin and other WASI-compliant runtimes (like WasmTime).
Here’s a detailed video by Joel Dice:
- The Bytecoder project cross-compiles Java to WebAssembly that can be executed in the browser
- The TeaVM project has experimental support for browser-based WebAssembly
- A dedicate WebAssembly compiler called JWebAssembly can translate any JVM bytecode to WebAssembly, including Groovy, Clojure, and Kotlin. It, too, is browser-centric.
- CheerpJ is much more ambitious, handling the UI as well
Learn More
Here are some great resources:
- WebAssembly for the Java Geek details how Wasm’s bytecodes and virtual machine differ from Java’s, and why this makes a difference.
- An in-depth blog showing practical ways to run Java as Wasm
- Does Wasm remind you of Java Applets? Then read this blog post
- GraalVM has gained a lot of momentum as a WebAssembly runtime, though it does not appear to support compiling from languages to WebAssembly.
- The New Stack wrote about Java and WebAssembly from a more historical perspective
- Fermyon posted about the differences between different languages and how that makes a difference with WebAssembly
- Joel Dice’s WasmDay talk on JVM and WASI
- Hands on with Java and Wasm at InfoWorld provides a TeaVM example targeted for the browser.