CheerpJ 2.0 released — now with WebAssembly support

Compile Java applications into WebAssembly and JavaScript

Stefano De Rossi
Feb 4 · 5 min read

After two years of further development, we are happy to announce the second major release of CheerpJ, our solution to automatically convert any Java application into HTML5 leveraging WebAssembly and JavaScript.

Example Java application converted to WebAssembly and JavaScript with CheerpJ (OpenAstexViewer)

The purpose of CheerpJ is to provide an automated tool for converting full legacy Java applications (including Applets and stand-alone Java clients) or parts of existing applications (usually the business logic) into a combination of WebAssembly and JavaScript, so they can be integrated into a modern, browser-based HTML5 web application.

CheerpJ achieves this by:

  1. Covering 100% of the Java 8 language, including reflection and dynamic class generation;
  2. Providing a full Java SE runtime library;
  3. Supporting file system access (read and write), audio, printing through native browser features;
  4. Supporting Java multi-threading and HTML5 WebWorkers;
  5. Converting Java from bytecode, without requiring access to the application source code.

CheerpJ combines an AOT Java bytecode compiler with a full Java runtime in WebAssembly and JavaScript, allowing virtually any Java application to be converted to JavaScript without manual intervention.

Differently from alternative solutions, CheerpJ does not require any change to be made to the source code of the application, supports 100% of the Java language and runtime, and is not a framework and thus in no way opinionated.

CheerpJ is available for Windows, macOS and Linux, and can be downloaded here.

How does CheerpJ work?

CheerpJ consists of three components: an AOT compiler, a full Java runtime in WebAssembly and JavaScript, and an interoperability API.

The CheerpJ AOT compiler, a Java-bytecode-to-JavaScript compiler based on LLVM as well as on the Cheerp C/C++ to JavaScript compiler;

The CheerpJ runtime components provide:

  1. A complete, modular, optimised Java 8 SE runtime library in WebAssembly and JavaScript;
  2. A module to allow transparent support for reflection and dynamic class generation;
  3. Browser-based implementations of the file system, audio, printing, and other ‘operating system’ features.

The CheerpJ JavaScript/DOM interoperability APIs allow:

  1. Access and manipulation of the DOM from Java;
  2. Implementation of ‘native’ Java methods in JavaScript;
  3. Access to converted Java libraries from HTML5/JavaScript.

How do I use CheerpJ?

Using CheerpJ to convert your Java application is straightforward:

  • Compile all your .jar archives using the CheerpJ AOT compiler;
  • Minimise your .jar archives (optional, removes all the Java bytecode from the packages);
  • Embed your converted application (or link, if a library) in a HTML page, and link to the CheerpJ runtime library.

CheerpJ does not require any server-side support, as all application components (converted application and runtime) are static. You can put a CheerpJ application behind a CDN to virtually cancel any bandwidth costs.

The CheerpJ runtime environment

CheerpJ includes a full Java 8 SE runtime environment, plus a collection of browser-based ‘native’ implementations of different functionalities.

The CheerpJ runtime environment is provided as a collection of WebAssembly and JavaScript files, and can be simply linked from our CDN, by adding a link to the top of an HTML page:

<script src=”https://cjrtnc.leaningtech.com/2.0/loader.js"></script>

The CheerpJ runtime consists of a combination of WebAssembly and JavaScript packages, which are loaded automatically on-demand from the CDN. Only the minimum number of packages required by the converted application will be downloaded by the loader script.

CheerpJ 2.0 introduces for the first time WebAssembly runtime modules, which are used whenever the runtime package is computationally heavy (e.g. font rendering).

The CheerpJ runtime provides extensive support to a number of operating system-like features, including:

  1. Virtual file systems implemented on top of IndexedDB (read-write access), HTTP(S) (read-only) and strings (read-only);
  2. Support for real-time audio (via WebAudio);
  3. Support for printing (via the browser printing dialogue);
  4. Support for WebWorkers, allowing the development of concurrent applications;
  5. Support for DOM manipulation, as well as invoking JavaScript from Java.

Why CheerpJ?

CheerpJ was developed with three main use-case scenarios in mind:

  1. The conversion of legacy Java applications and Java Applets to HTML5 with minimal or no effort, to extend their life until deprecation, or until a replacement native HTML application has been developed;
  2. The migration of an existing Java client to a browser-based web application, usually by converting the business logic from Java to WebAssembly/JavaScript with CheerpJ, and rewriting the UI in native HTML;
  3. Using (converted) Java libraries as part of a native web application.

The majority of the commercial users of CheerpJ fall into the first two categories. CheerpJ is not intended for writing a web application from scratch, except in the case exemplified in point 3 above. We do not see CheerpJ as a Blazor-equivalent for Java, although anyone is welcome to build a framework on top of it.

CheerpJ and performance

BlackBird, a cloud and browser-based video editor made with CheerpJ.

CheerpJ is actively used by a number of commercial applications, as well as in scientific, educational and academic institutions. Many of these applications have stringent performance requirements.

CheerpJ adopts a great range of optimisations to ensure great runtime performance, and small-as-possible download footprint in converted applications:

  1. Modularised Java 8 runtime. Only the required components of the runtime are downloaded, to minimise the total download footprint. A special debugging feature allows tracking the methods originating the request of each runtime module, to allow further optimisations on the application side.
  2. Highly-optimised code generation, based on LLVM full-code optimisations. CheerpJ is a full compiler based on LLVM, and as such, it leverages all the full-code optimisations of the LLVM compiler stack.
  3. Removal of bytecode from .jar archives. CheerpJ requires the original .jar archives to be deployed alongside the application to support reflection and dynamic class generation. However, only the archive structure and metadata are preserved, significantly minimising the download size.

Full Changelog — CheerpJ 2.0

  • Introduction of WebAssembly runtime modules to improve runtime speed and reduce the size of packages with heavy computational cost (e.g. font rendering)
  • Extended file system support to read, write and delete (IndexedDB backend)
  • New read-only filesystem in /str/ for Strings or binary data
  • Improved event handling, mouse interaction and focus
  • Extend support for charsets
  • Improved robustness to invalid classes often present in real-world JAR archives
  • Improved isolation of CheerpJ symbols to avoid collisions with JavaScript identifiers in real-world HTML pages (particularly important if used in a front-end framework such as Angular or React)
  • Improved support to multi-threading, in particular Thread.interrupt
  • Use the CFGStackifier algorithm to synthesize control flow in generated code
  • New API to receive preloading notification (allows to build custom progress bars)
  • Improved detection of Applets on HTML pages
  • Improved packaging for MacOS

Getting started

You can find the Documentation for CheerpJ, together with tutorials and examples, on the CheerpJ Documentation page.

To report any issues, please use our issue tracker. For any question or comment, you can find us on the CheerpJ Gitter channel.

Thank you!

Leaning Technologies

leaningtech

Leaning Technologies' Blog - everything Cheerp, CheerpJ…

Thanks to Carlo Piovesan

Stefano De Rossi

Written by

leaningtech

Leaning Technologies' Blog - everything Cheerp, CheerpJ, compile-to-JavaScript and compile-to-WebAssembly

More From Medium

More from leaningtech

More on Webassembly from leaningtech

More on JavaScript from leaningtech

More on JavaScript from leaningtech

ViewSource 2019: conference report

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade