Load a Java Library

Allows to load a Java Library into JavaScript.

async function cheerpjRunLibrary(classPath: string): Promise<CJ3Library>;


  • classPath (string) - The path to the library’s jar file in the virtual filesystem. Pass an empty string to load the standard library only.


cheerpjRunLibrary returns a Promise which resolves to a CJ3Library object.


This object can be used to access the classes and methods of the loaded library through property access:

  • To load a class, access it and await it.
  • To call a static method, call it as a method on a loaded class and await it.
  • To construct a class into an instance, use await new.
  • To call an instance method, call it as a method on an instance of a loaded class and await it.

Array interoperability is supported:

  • Passing JS arrays to Java arrays via copying.
  • Passing JS arrays to Java JSObject.
  • Passing Java object arrays to JavaScript by reference.
  • Passing Java primitive arrays to JavaScript
  • Public Java fields can now be both read/written from JS with appropriate type conversion.
  • Classes in JS supports instanceof.

Parameters and return values of method calls are automatically converted between JavaScript and Java types.


Using the standard library

await cheerpjInit();
const lib = await cheerpjRunLibrary("");
const System = await;
await System.out.println("Hello from Java");

Using a custom library

Let’s say we had a library called example.jar compiled from the following class:

package com.example;
public class Example {
public String[] greetings = {"Hello", "Bye"};
public void hello() {
System.out.println("Example says hello!");

With example.jar being available on the web server at /example.jar, we could use it like so:

await cheerpjInit();
const lib = await cheerpjRunLibrary("/app/example.jar");
const Example = await;
const example = await new Example();
await example.hello(); // Example says hello!

Access to object property/attribute

From the example above:

const greetingsArray = example["greetings"];


const greetingsArray = example.greetings;

Exception handling

await cheerpjInit();
const lib = await cheerpjRunLibrary("");
try {
// Attempt to load a class that doesn't exist
} catch (e) {
await e.printStackTrace(); // java.lang.ClassNotFoundException: java.lang.DoesntExist
Was this page helpful?
Suggest changes