Java WebAssembly: Run Java in the Browser with CheerpJ

Run Java with WebAssembly in the Browser

Java can run in a modern browser without a plug-in or a local Java installation. CheerpJ provides a WebAssembly-based Java Virtual Machine (JVM) and OpenJDK runtime that execute standard Java bytecode inside the browser. Existing JAR files can therefore be delivered through a web page while the application remains written and maintained in Java.

This approach is different from converting an application’s Java source code directly into a WebAssembly module. CheerpJ brings a JVM to the browser. The JVM and runtime use WebAssembly and JavaScript, while the application continues to run as Java bytecode.

Can Java run with WebAssembly?

WebAssembly is a portable binary instruction format supported by modern browsers. It provides a compilation target for languages and runtimes that were not originally designed for the web. A Java application, however, normally compiles to JVM bytecode rather than directly to WebAssembly.

A WebAssembly-based JVM closes that gap. CheerpJ’s runtime and JVM are compiled for the browser using WebAssembly and JavaScript. The browser loads that runtime, which then reads and executes the bytecode in existing class and JAR files. Developers do not need to convert the application source into JavaScript or rewrite the application for a JavaScript framework.

How CheerpJ runs Java in the browser

CheerpJ is added to a web page through a script and initialised in the browser. It provides the JVM, OpenJDK runtime and browser-facing services required by many Java applications. Bytecode starts in an interpreter and can be just-in-time compiled to optimised JavaScript as it runs.

Component Role
OpenJDK runtime Provides the Java SE classes and services expected by Java applications.
JVM and execution engine Reads Java bytecode, begins execution in an interpreter and uses just-in-time compilation during execution.
Window manager Maps Swing and AWT interfaces to browser technologies such as HTML elements and canvas.
Virtual file system and networking Connects Java file and network operations to browser-compatible storage and transport mechanisms.
JavaScript interoperability Allows JavaScript to call Java classes and methods, and Java code to interact with JavaScript and the web page.

What developers can run

CheerpJ can support several browser delivery patterns without changing the application language:

  • Existing Java applications distributed as JAR files, including applications with a main class.
  • Swing and AWT desktop interfaces rendered inside a browser tab.
  • Java libraries called from JavaScript or TypeScript applications.
  • JNLP applications that previously depended on Java Web Start.
  • Java 8 applets that previously depended on a browser plug-in.

The amount of integration work varies by application. Pure Java code is usually the simplest case. Applications that depend on native libraries, direct operating-system access or specialised network behaviour should be assessed before deployment.

Use Java libraries from JavaScript

Java WebAssembly is not limited to displaying an existing desktop interface. A web application can load a Java library and call its classes and methods from JavaScript. This allows developers to reuse established Java logic, parsers, document tools or domain-specific libraries inside a browser application.

Interoperability also works in the other direction. Java code can call JavaScript functions and interact with the surrounding page. This makes it possible to combine Java components with a new HTML, CSS and JavaScript interface rather than moving an entire desktop application into the browser unchanged.

See the Java library guide and the Java and JavaScript interoperability tutorial for implementation examples.

Supported Java versions

CheerpJ provides extensive compatibility with Java 8, Java 11 and Java 17. This allows teams to evaluate browser delivery for both older enterprise applications and newer Java code. Java applet support applies to Java 8 applets.

Version compatibility is only one part of application compatibility. Dependencies, native integrations, file access, network requirements and user-interface behaviour can also affect the result. Test the actual application and its operational workflows before planning a production deployment.

How to run a Java application in the browser

A basic CheerpJ application deployment follows four steps:

  1. Collect the application JAR files and the dependencies required at runtime.
  2. Create an HTML page that loads CheerpJ and initialises the runtime.
  3. Start the application by running a JAR or main class. Add a display container when the application uses Swing or AWT.
  4. Host the HTML, JAR files and other application resources on an HTTP server and test them in the target browsers.

Follow the current CheerpJ application guide for the loader, initialisation and launch syntax.

When Java WebAssembly is useful

A browser JVM is worth evaluating when an organisation needs to:

  • Deliver an existing Java application through a URL instead of installing Java on every endpoint.
  • Keep a working Java codebase while changing how users access the application.
  • Run a Swing or AWT application in a modern browser.
  • Reuse a Java library inside a JavaScript or TypeScript web application.
  • Replace a browser plug-in or Java Web Start deployment with browser-based delivery.

It is not an automatic replacement for every Java deployment. Applications with JNI dependencies, native executables, unusual protocol requirements or heavy access to the local operating system may need adaptation. Performance and usability should be measured against the application’s real workloads rather than assumed from the technology alone.

For Java desktop interfaces, see Run Java Swing applications in the browser.

Frequently asked questions

Does CheerpJ compile my Java application directly to WebAssembly?

No. CheerpJ supplies a JVM and OpenJDK runtime built for the browser using WebAssembly and JavaScript. The application remains Java bytecode and can run from its existing JAR files.

Do I need the Java source code?

Not for a standard JAR-based deployment. CheerpJ works with Java bytecode, so source code is not required simply to run an existing application. Source access may still be useful if the application needs changes or integration work.

Does the user need Java installed?

No. The Java runtime is delivered as part of the browser application. Users access it through a supported browser rather than installing a desktop JRE or browser plug-in.

Can CheerpJ run existing JAR files?

Yes. Existing applications and libraries can be loaded from JAR files. Compatibility depends on the application’s dependencies and use of browser-restricted operating-system features, so testing remains necessary.

Which Java versions are supported?

CheerpJ provides extensive compatibility with Java 8, 11 and 17. Use the current documentation to confirm version-specific requirements and test the application before deployment.

Next steps

Start with the deployment model that matches the application. Run an existing JAR as a browser application, load a Java library from JavaScript, or evaluate a Swing interface in the browser. The current CheerpJ documentation covers each route.

Run a Java application

Use a Java library from JavaScript

Build Java and JavaScript interoperability

Evaluate Java Swing in the browser

More from CheerpJ