Return of the applet? CheerpJ 3.0 runs a full JVM in HTML5 and WebAssembly

Return of the applet? CheerpJ 3.0 runs a full JVM in HTML5 and WebAssembly

Learning Technologies will release CheerpJ 3.0 later this year, an implementation of the JVM (Java Virtual Machine) in HTML5 and WebAssembly, including a JIT (just in time) compiler and full Classloader support.

The primary target is enterprises with existing Java applications. Another use case is for teaching, with a “JavaFiddle” application that lets users type and run Java code entirely in the browser. “The demo takes advantage of the fact that the java compiler itself is written in Java, and hence can run in CheerpJ as well,” the company said.

The new product will replace the existing CheerpJ, also a JVM implementation in WebAssembly, but which according to the company has shortcomings that could only be fixed by a new architecture. There were several issues. One was that it required Java to be compiled ahead of time. “The need to add CheerpJ as a post-processing step in a CI setup was often felt as an unwelcome burden,” the company said. Another issue was that it was stuck on Java 8.

According to the company, “CheerpJ is based on an unmodified OpenJDK environment, guaranteeing the same behavior on the browser compared to a native JVM. It includes many emulation layers to ensure Filesystem, Networking, Printing, Clipboard and many other subsystems work seamlessly.”

JavaFiddle, a demo of CheerpJ 3.0

We found it easy to amend, compile and run a sample Swing application (the standard GUI framework for Java) in the JavaFiddle environment, though even the few short lines was slow to compile and run.

There are other ways to run Java in the browser. TeaVM requires ahead-of-time compilation and outputs JavaScript and WebAssembly from Java class files. JWebAssembly is a Java bytecode to WebAssembly compiler but “not a complete VM with GC and memory management.”

Google’s j2cl transpiles from Java to “Closure style JavaScript” and is used by Google for Workspace, despite which Google’s J2CL team says “J2CL is not an official Google product and is currently in ‘alpha’ release for developer preview.” J2CL is interesting in this context since one of the things on its to-do list is to “Improve WASM GC spec, tooling and J2CL experimental Wasm backend”. GC (Garbage Collection) is a big issue for implementing languages like Java and C# in WebAssembly. Currently it is necessary for each implementation to supply its own GC, adding to the file size as well being less efficient than native GC would be.  

There is also a project to add Wasm as a GraalVM target. GraalVM is the official Java solution for compiling to native code. The GraalVM team is also waiting for GC to be solved, but according to a comment on this issue, “GC proposal made a lot of progresses in 2022 and moved to phase 3. We can’t be sure when it will move to phase 4 but could be possible in 2023.”

Running Java in the browser has an attraction for enterprises with legacy Java applications, which perhaps can now be browser applications thanks to a plugin like this. That said, while running Swing in the browser is an impressive trick, it is far from ideal from a user interface perspective. It is better of course to implement the UI using web technology; and once developer teams start on that task, they may also be asking themselves whether running Java in Wasm, with its complications and compromises, is really the best approach today.

The CheerpJ product is not open source. It is free for “testing and technical evaluation” but the UK-based company says licenses start from £80 per month for commercial usage.

Update

At the Google I/O event there was news about WebAssembly GC, also called WasmGC, which is an in-progress proposal. WebAssembly GC means that garbage collection is built into the browser for WebAssembly code as well as for JavaScript, so languages that use GC no longer need to ship their own implementation. The feature also improved the speed of interop between WebAssembly and the browser DOM (Document Object Model).

According to Google’s presentation, Dart (used by Flutter) and Kotlin now run twice as fast when compiled to WebAssembly, as when compiled to JavaScript. WebAssembly GC is currently only available in Chromium-based browsers as an experimental feature.