Build with CheerpJ

Interoperability

CheerpJ runs Java inside the web page, on the user’s own computer. Java and JavaScript call each other directly, with no server in between, and Java code reaches files, the network and the browser through the page.

At a glance

Java and JavaScript, in the same page

JavaScript to Java

Load JAR files and use their classes, objects and arrays from JavaScript or TypeScript, with async and await.

Java to JavaScript

Implement Java native methods in JavaScript, so Java code can call JavaScript functions and libraries.

Java to the browser

Files, the network, the application’s display, the clipboard and external commands all go through the browser.

JavaScript to Java

Java from JavaScript: library mode

In library mode, JavaScript loads Java classes, creates Java objects and calls their methods directly. Java libraries can be used in a web application alongside the rest of its code.

await cheerpjInit();
const lib = await cheerpjRunLibrary("/app/library.jar");
const ArrayList = await lib.java.util.ArrayList;
const list = await new ArrayList();
await list.add("Hello from JavaScript");

What JavaScript can do

  • Load only the Java standard library, or the standard library and your JAR files
  • Create objects with await new, and call static and instance methods
  • Read and write fields and array elements directly
  • Test Java types with instanceof, and catch Java exceptions in JavaScript

Example: a PDF from JavaScript

The library mode guide loads the iText library, builds a PDF in Java, and shows it in the page with a few lines of JavaScript.

Java to JavaScript

JavaScript from Java: native methods

Java native methods can be implemented in JavaScript. Java code calls them as it would any other method, so it can use JavaScript libraries and anything the browser offers.

Native methods

Name an async function after the Java method, Java_<class>_<method>, and pass it to CheerpJ when it starts. It receives the library object and the Java object it was called on.

async function Java_Page_setTitle(lib, self, title) {
	document.title = title;
}
await cheerpjInit({ natives: { Java_Page_setTitle } });

Native libraries

System.loadLibrary loads a JavaScript module from java.library.path, and the native methods it exports can be shared across the whole application.

export default {
	async Java_Example_alert(lib, message) {
		window.alert(message);
	},
};

A native method can call back into Java through the library object it receives. For two-way communication at any time, a long-running Java thread keeps the channel open, as the interoperability tutorial shows.

Type conversion

Values passed between Java and JavaScript

Parameters and return values are converted automatically in both directions, following the LiveConnect rules. Typed arrays are passed by reference, so Java and JavaScript share the same data without copying it.

JavaScript Java Passed
Numbers, strings and booleans Primitive types and StringBy value, following LiveConnect
Int8Array byte[]By reference
Uint8Array boolean[]By reference
Uint16Array and Int16Array char[] and short[]By reference
Int32Array and BigInt64Array int[] and long[]By reference
Float32Array and Float64Array float[] and double[]By reference
Any other value netscape.javascript.JSObjectAs an opaque reference

Files

A virtual filesystem, shared with JavaScript

CheerpJ gives the Java application a virtual filesystem, and JavaScript can use it too. The browser does not let web pages read or write files on the user’s computer directly, so files enter and leave through uploads and downloads.

Mount point Contents Java JavaScript
/app/ Files on your web server, such as the application's JAR files and data, read over HTTPReadRead
/files/ The application's own files, kept in the browser's storage between sessionsRead and writeRead
/str/ Text or binary data passed in from JavaScript, kept in memoryReadWrite

Into the application

  • The upload button on every Java window’s title bar puts the user’s file in /files/uploads/, with no code
  • cheerpOSAddStringFile passes text or binary data from JavaScript, such as a file from an <input type="file">
  • Library mode can copy files from /app/ to /files/

Out of the application

  • Files that Java saves in /files/downloads/ are downloaded to the user’s computer automatically
  • cjFileBlob reads any file as a Blob, to download, display or upload it from JavaScript
  • A native method lets Java code decide when to start a download

Network

Java networking, through the browser

Java networking code runs unchanged. CheerpJ carries its connections over the browser’s own networking.

HTTP and HTTPS

Java’s HTTP connections are made with the browser’s fetch. Same-origin policy and CORS apply, and HTTP requests are upgraded to HTTPS on HTTPS pages.

TCP and UDP sockets

Browsers do not give web pages raw sockets. CheerpJ provides them through Tailscale, configured when CheerpJ starts with an auth key or a login callback, and a self-hosted Headscale control server if you use one.

Custom networking backends

Enterprise deployments can connect CheerpJ to a custom networking backend, for example to go through their own proxies or to use other transports such as WebSockets.

How the application connects to your back-end services, networks and sign-in, and where it can be hosted, is on the compatibility page.

Browser and Web APIs

The browser, from Java

  • Any Web API

    A native method gives Java code any API the browser offers, such as geolocation, notifications, messaging between pages, and serial or USB devices where browsers support them.

  • The display in your page

    cheerpjCreateDisplay places the application’s windows inside any element, at a fixed size or filling it, including in pages built with frameworks such as React.

  • Clipboard

    The clipboard stays inside the Java application by default. It can be shared with the system clipboard, with or without asking the user’s permission.

  • External commands

    execCallback passes Runtime.exec and ProcessBuilder commands to JavaScript, so a command that opens a URL can open a browser tab instead.

  • Configuration from the page

    Java system properties and the document base URL are set from JavaScript when CheerpJ starts.

Applets

Applets and their page

Applets keep working with the page they are embedded in.

Tags run in place

CheerpJ finds the page’s <applet>, <object> and <embed> tags and runs each applet where its tag is, on Java 8.

Applet parameters

The applet reads its parameters from the page as before. appletParamFilter can rewrite them when CheerpJ starts, for example to change a server URL.

Calling the page

AppletContext.showDocument opens URLs, and javascript: URLs call the page’s JavaScript functions. Native methods cover anything else.

Native code

JNI and native libraries

OpenJDK’s native code

OpenJDK’s own JNI code is compiled unmodified from OpenJDK to WebAssembly, so the Java class library behaves as it does on the desktop.

The application’s native code

Native methods run when they are compiled to WebAssembly or written in JavaScript. Pre-built platform binaries, such as Windows DLLs, cannot run in a browser.

Questions

Ask us about your application

Our engineers can answer questions about integrating CheerpJ with your web application.

Build with Java for the browser.