TeaVM

TL;DR: We’re excited to announce that WebFX now officially supports TeaVM, a Java to WebAssembly compiler that unlocks faster startup times and broader JVM language compatibility with now Kotlin and Scala!

We already have multiple live demos running with TeaVM, which you’ll find below.


What is TeaVM?

TeaVM is a compiler that translates Java bytecode into JavaScript or WebAssembly. This is a key difference from GWT, which transpiles Java source code directly into JavaScript.


Why TeaVM support matters

  • WebAssembly performance

Unlike JavaScript, WebAssembly (Wasm) is a low‑level binary format already compiled before it reaches the browser, allowing near‑native performance and now supported by all modern browsers.

  • Kotlin and Scala support

Thanks to its bytecode‑based design, TeaVM supports not only Java but also Kotlin and Scala right out of the box. This expands WebFX beyond Java, allowing teams to choose their preferred language without giving up JavaFX as their cross-platform UI toolkit.

  • Future-proofing WebFX

The language-neutral design of its bytecode (as opposed to Java) is a major reason for WebAssembly’s growing popularity. While JS won’t disappear, it’s likely that more and more web apps will be written in other languages and compiled to Wasm in the future.

By supporting Wasm today, WebFX is already aligned with this shift and part of the emerging “post-JavaScript” era, while still remaining fully compatible with JS.


Why we chose TeaVM among Java → Wasm compilers

While several Java-to-Wasm projects exist, we found that TeaVM was the most practical and lightweight solution for Java web development:

Wasm compiler Status Notes
TeaVM Active Lightweight, mature, supports Wasm GC with DOM API
GraalVM (Oracle) Active Heavy output, limited to web workers (no DOM API)
Kotlin/Wasm Active Kotlin sources only - unusable with JavaFX
CheerpJ Active Embeds mini JVM, large binaries
J2CL/Wasm (Google) Active Internal opaque project (very few com and docs)
ByteCoder Inactive Stagnant since 2024
JWebAssembly Inactive Stagnant since 2023

Among these, GraalVM is probably TeaVM’s most serious competitor, especially given that it’s backed by Oracle. However, GraalVM has historically focused on the execution of Wasm modules in the JVM (GraalWasm), mainly on the server side. Its Java → Wasm compiler and browser support is only nascent, lacks direct DOM access, and currently produces much larger binaries.

We may add GraalVM support to WebFX if its browser target matures. But for now, TeaVM appears to offer the most advanced, lightweight, and browser-ready Java → WebAssembly solution available.


How we integrated TeaVM into WebFX

To make TeaVM compatible with the large existing WebFX codebase whose browser interactions are coded with Elemental2, the Google library that exposes browser APIs (DOM, events, storage, etc.) to Java, we created a polyfill of Elemental2 that allows TeaVM to seamlessly understand and compile the same Elemental2-based code used by GWT.

Polyfill

For GWT/J2CL developers: this polyfill covers only the subset of the Elemental2 API used by WebFX for now. However, it can be expanded to cover additional Elemental2 APIs as needed. Increasing the coverage is a straightforward (if somewhat repetitive) task, so in principle, it could compile any Elemental2-based project to Wasm with TeaVM.

And it works! You can now build the same WebFX codebase with either GWT or TeaVM with no changes at all to your application code.


Try it out: WebFX + TeaVM live demos 🔥

Each demo below is built from the same JavaFX codebase compiled with GWT (JavaScript) and TeaVM (JavaScript and WebAssembly), so you can compare them side by side. Their source code is available in our demos space.

WebFX Demo GWT (JS only) TeaVM - JS TeaVM - Wasm 🤩
Enzo Clocks EnzoClocks-GWT EnzoClocks-TeaVM EnzoClocks-Wasm
Modern Gauge ModernGauge-GWT ModernGauge-TeaVM ModernGauge-Wasm
Cube Cube-GWT Cube-TeaVM Cube-Wasm
Mandelbrot Mandelbrot-GWT Mandelbrot-TeaVM Mandelbrot-Wasm
DemoFX DemoFX-GWT DemoFX-TeaVM DemoFX-Wasm
SpaceFX SpaceFX-GWT SpaceFX-TeaVM SpaceFX-Wasm
JArkanoid JArkanoid-GWT JArkanoid-TeaVM JArkanoid-Wasm
Tetris Tetris-GWT Tetris-TeaVM Tetris-Wasm
FX2048 FX2048-GWT FX2048-TeaVM FX2048-Wasm
Food Dice FoodDice-GWT FoodDice-TeaVM FoodDice-Wasm
Pac-Man PacMan-GWT PacMan-TeaVM PacMan-Wasm
MsPac-Man MsPacMan-GWT MsPacMan-TeaVM MsPacMan-Wasm

Note: TeaVM compiles to Wasm GC, the new WebAssembly standard designed for garbage-collected languages like Java. This feature is currently supported by modern browsers such as Chrome, Edge, Firefox, and Safari (recent versions only).


What’s next?

WebFX Wasm is already usable today with modern browsers, and we’re now planning to support dual TeaVM/GWT builds for a reliable transition. WebFX will select the appropriate version at runtime, giving the best of both worlds: WebAssembly where possible, or full JS backward compatibility where needed. So stay tuned for more updates as we refine our TeaVM integration!