JavaScript to Java
Load JAR files and use their classes, objects and arrays from JavaScript or TypeScript, with async and await.
Build with CheerpJ
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
Load JAR files and use their classes, objects and arrays from JavaScript or TypeScript, with async and await.
Implement Java native methods in JavaScript, so Java code can call JavaScript functions and libraries.
Files, the network, the application’s display, the clipboard and external commands all go through the browser.
JavaScript to Java
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");await new, and call static and instance methodsinstanceof, and catch Java exceptions in JavaScriptThe 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
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.
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 } }); 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
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 String | By 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.JSObject | As an opaque reference |
Files
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 HTTP | Read | Read |
| /files/ | The application's own files, kept in the browser's storage between sessions | Read and write | Read |
| /str/ | Text or binary data passed in from JavaScript, kept in memory | Read | Write |
/files/uploads/, with no codecheerpOSAddStringFile passes text or binary data from JavaScript, such as a file from an <input type="file">/app/ to /files//files/downloads/ are downloaded to the user’s computer automaticallycjFileBlob reads any file as a Blob, to download, display or upload it from JavaScriptNetwork
Java networking code runs unchanged. CheerpJ carries its connections over the browser’s own networking.
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.
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.
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
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.
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.
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.
execCallback passes Runtime.exec and ProcessBuilder commands to JavaScript, so a command that opens a URL can open a browser tab instead.
Java system properties and the document base URL are set from JavaScript when CheerpJ starts.
Applets
Applets keep working with the page they are embedded in.
CheerpJ finds the page’s <applet>, <object> and <embed> tags and runs each applet where its tag is, on Java 8.
The applet reads its parameters from the page as before. appletParamFilter can rewrite them when CheerpJ starts, for example to change a server URL.
AppletContext.showDocument opens URLs, and javascript: URLs call the page’s JavaScript functions. Native methods cover anything else.
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.
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
Our engineers can answer questions about integrating CheerpJ with your web application.