CheerpJ 3.0 - Run Java Apps Inside The Browser
Written by Nikos Vaggalis   
Thursday, 08 February 2024

CheerpJ is a Java-to-Javascript runtime and compiler that can convert any Java application to HTML5 for in-browser rendering. Version 3.0 has been fully rewritten from scratch.

Wouldn't be awesome to be able to run Swing, legacy Java GUI apps, or Applets inside the browser?

CheerpJ's, according to its developers Leaning Technologies, use-case scenarios are threefold :

  • 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.

  • 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.

  • Using (converted) Java libraries as part of a native web application.

If you thought that legacy apps are not used anymore in this day and age, think again. A huge market lies behind corporations still , say, on Oracle Forms with Java 8 and Applets which in order to run need a special ESR Firefox browser that still supports the java plugin.

Many Enterprises are using such legacy stacks and still talk Java. For instance the majority of Fortune 500 list of companies do, and the reasons for that are plenty. The most important, and the one that enterprises value most, is backwards compatibility, since they are notoriously allergic to radical updates and upgrades. Systems that worked 20 years ago, written in Java 5, should be able to compile and run under version 8. Stability is what matters.

So being able to port such apps to modern times and run them inside any browser with no changes in the underlying infrastructure, is something which is in great demand. As such CheerpJ's main target group is real-world Enterprise applications.

To support such a crucial endeavor, CheerpJ comes with a set of other features:

  • No server side component is required. All the code is run on the client via WebAssembly and JavaScript.

  • Works from unmodified JAR files (no need for source code), no need for any compilation or preprocessing step. Works with obfuscated bytecode, independently of the obfuscator being used.

  • Supports very large applications, our stress test is IntelliJ IDEA 2019 which ships ~400MBs of JARs. Check it out.

  • Supports graphical applications, both AWT- and Swing-based ones are supported, including third-party Look&Feels. Multiple applications, each with multiple windows, can run at the same time.

  • Full support for reflection and classloaders, including custom ones designed to support plugins or encrypted JARs.

And of course interoperability with JavaScript:

Library mode
Library mode allows you to directly use Java methods, objects, and arrays from JavaScript. This API has been designed to take advantage of async/await to feel more natural to use, without sacrificing any flexibility. Loading a library is as simple as calling cheerpjRunLibrary. 

As a practical example, these few lines of code make it possible to generate a PDF from JavaScript using the popular iText library:

JavaScript natives
You can implement Java native methods (JNI) directly in JavaScript with a convenient interface.
This feature can also be used to interact with JavaScript and DOM APIs from Java. It is possible to implement Java ‘native’ methods (that would normally be implemented in C/C++ or other AOT-compiled language) in JavaScript, similarly to what would be done in regular Java using the Java Native Interface (JNI).

Of course if you want to port a C/C++ application to the web, there's also Cheerp C++ that lets you compile virtually any C/C++ code to WebAssembly and JavaScript.

While primarily used to port existing C/C++ libraries and applications to HTML5, it can also be used to write high-performance Web applications and WebAssembly components from scratch. Using Cheerp, C/C++ code gets compiled into JavaScript/WebAssembly as well as becoming optimised with the right JavaScript interfaces being exposed, and can be easily integrated in a web application.

The C++ version unlike the Java one, is also open source as we examined in Cheerp C++ To Webassembly Compiler Now Open Source. Still despite CheerpJ being closed source, it however is free for personal use.

At this point it is also important to note that a current limitation of CheerpJ is that it runs only on Java 8, which might be sufficient for running legacy applet based applications but is not cut for the modern stack. However on the project's roadmap is to support multiple Java versions as well as multiple runtime environments.

The Cheerp family of runtimes comes as an addition to other ways of running WebAssembly applications nowadays.
One such alternative as we've examined in Running PostgreSQL Inside Your Browser, is by building a v86 virtual machine based on an Alpine Linux image and installing Postgres on it so that it could run inside the browser, since v86 emulates an x86-compatible CPU and hardware by translating machine code to WebAssembly at runtime.

Another alternative which we've recently covered in Wasmer JS SDK - WebAssembly In The Browser is through WASI(X) of Wasmer:

WASIX is an extension to the WebAssembly System Interface (WASI) designed to make WebAssembly (Wasm) more compatible with POSIX programs, enabling the seamless execution of more complex applications in both the browser and server environments.

To experience yourself what WebAssembly is capable of doing, check this example of Browsercraft which is a proof-of-concept demo of Minecraft 1. 2. 5 running unmodified in the browser, powered by CheerpJ.

There is also an online playground, where you can write Java code and see it rendered in real time with CheerpJ, as well as the CheerpJ Applet Runner Chrome plugin with which you can run legacy Java Applets on Chrome without having to install Java.

 

More Information

CheerpJ 3.0
Browsercraft

 

Related Articles

Wasmer JS SDK - WebAssembly In The Browser

Cheerp C++ To Webassembly Compiler Now Open Source

Running PostgreSQL Inside Your Browser

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


ZLUDA Ports CUDA Applications To AMD GPUs
18/04/2024

ZLUDA is a translation layer that lets you run unmodified CUDA applications with near-native performance on AMD GPUs. But it is walking a fine line with regards to legality.



Vesuvius Challenge Continues
28/04/2024

The Vesuvius Challenge is a machine learning and computer vision competition which started in March 2023. Its overarching aim is to read the contents of physically impenetrable Herculaneum Papyri burn [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 08 February 2024 )