cheerpjInit
Set up and initialize the CheerpJ runtime environment
cheerpjInit
must be called once in the page to setup and initialise the CheerpJ runtime environment.
async function cheerpjInit(options?: { version?: number; status?: "splash" | "none" | "default"; logCanvasUpdates?: boolean; preloadResources?: { [key: string]: number[] }; preloadProgress?: (preloadDone: number, preloadTotal: number) => void; clipboardMode?: "permission" | "system" | "java"; beepCallback?: () => void; enableInputMethods?: boolean; overrideShortcuts?: (evt: KeyboardEvent) => boolean; appletParamFilter?: (originalName: string, paramValue: string) => string; natives?: { [method: string]: Function }; overrideDocumentBase?: string; javaProperties?: string[]; tailscaleControlUrl?: string; tailscaleDnsUrl?: string; tailscaleAuthKey?: string; tailscaleLoginUrlCb?: (url: string) => void; fetch?: ( url: string, method: string, postData: ArrayBuffer, headers: unknown[], ) => Promise<unknown>;}): Promise<void>;
Parameters
- options (
object
, optional) - Used to configure different settings of the CheerpJ runtime environment in the form{ option: "value" }
.
Returns
cheerpjInit
returns a Promise which is resolved when the CheerpJ runtime environment is ready to be used.
Options
A description of each cheerpjInit()
option with brief examples are given below.
version

version?: number;
The Java runtime version to use. 8
is the only supported value at the moment.
status

status?: "splash" | "none" | "default";
This option determines the level of verbosity of CheerpJ in reporting status updates.
"default"
: Enables status reporting during initialization and short-lived “Loading…” messages every time new runtime code is being downloaded."splash"
: Enabled status reporting only during initialization. There will be no feedback after the first window is shown on screen."none"
: Disable all status reporting.
Example:
cheerpjInit({ status: "splash" });
logCanvasUpdates

logCanvasUpdates?: boolean;
When set to true
, it enables logs on the console about the display areas which are being updated. Useful to debug overdrawing.
Example:
cheerpjInit({ logCanvasUpdates: true });
preloadResources

preloadResources?: { [key: string]: number[] };
By using preloadResources
, you can provide CheerpJ with a list of runtime files which you know in advance will be required for the specific application. The list should be given as a JavaScript array of strings.
Example:
cheerpjInit({ preloadResources: {"/lts/file1.jar":[int, int, ...], "/lts/file2.jar":[int,int, ...]} });
See also cjGetRuntimeResources.
preloadProgress

preloadProgress?: (preloadDone: number, preloadTotal: number) => void;
This callback may be used in combination with preloadResources
to monitor the loading of an application. The information provided is useful, for example, to display a loading/progress bar.
Example:
function showPreloadProgress(preloadDone, preloadTotal) { console.log("Percentage loaded " + (preloadDone * 100) / preloadTotal);}
await cheerpjInit({ preloadProgress: showPreloadProgress });
clipboardMode

clipboardMode?: "permission" | "system" | "java";
This option allows you to configure how the clipboard will behave. Supported clipboard modes are java
, system
and permission
.
Example:
cheerpjInit({ clipboardMode: "system" });
java
mode
This is the default setting. CheerpJ supports an internal clipboard which is local to the Java application and is not integrated with the system clipboard.
system
mode
In system
mode CheerpJ will share the clipboard with the system. Browsers enforce serious limitations on how the system clipboard can be accessed. In practice it is generally accessible when the Ctrl+C
and Ctrl+V
shortcuts are used (Cmd+C
and Cmd+V
on MacOSX). Due to these limitations the UX when using clipboardMode:"system"
is:
Ctrl+C
/Cmd+C
: the user has to press the shortcut twice to give CheerpJ access to the system clipboard. CheerpJ will block the execution while waiting for the secondCtrl+C
.Ctrl+V
/Cmd+V
: this shortcut behaves normally, there is no difference with native execution.- Menu based Copy/Paste:
Ctrl+C
/Ctrl+V
are needed to access the clipboard. CheerpJ will block the execution while waiting the appropriate shortcut.
permission
mode
With permission
mode enabled, CheerpJ offers a more seamless integration than system
mode. This includes support for images and HTML on top of plain text. Another important feature is that pressing extra Ctrl+C
/Ctrl+V
to perform copying and pasting operations is not required.
The first time an operation is requested, the user will be asked for permission to use the clipboard. If this permission is refused, a message will be prompted explaining this permission is necessary to continue.
This mode is experimental and might show a few inconsistencies in some browsers.
See more
.
enableInputMethods

enableInputMethods?: boolean;
When this option is set to true
CheerpJ will be able to receive text input from the input method framework of the platform. This is useful to support text input for languages such as Chinese, Japanese and Korean.
cheerpjInit({ enableInputMethods: true });
overrideShortcuts

overrideShortcuts?: (evt: KeyboardEvent) => boolean;
Some applications needs to internally handle keyboard shortcuts which are also used by the browser, for example Ctrl+F. Most users expect the standard browser behavior for these shortcuts and CheerpJ does not, by default, override them in any way.
A CheerpJ-compiled application can take control of additional shortcuts by providing a callback function as the overrideShortcuts
options of cheerpjInit
. This callback receives the KeyboardEvent
coming from the browser and should return true
if the default browser behaviour should be prevented.
Whenever possible we recommend not to use browser reserved shortcuts, to maintain a consistent user experience. In any case, the following limitations apply:
- Some shortcuts (Ctrl+T, Ctrl+N, Ctrl+W) are reserved by the browser and never received by the page itself. These cannot be overridden
- Overriding (Ctrl+C/Ctrl+V) will prevent
clipboardMode:"system"
from working correctly.
Example:
cheerpjInit({ overrideShortcuts: function (e) { // Let Java handle Ctrl+F if (e.ctrlKey && e.keyCode == 70) return true; return false; },});
appletParamFilter

appletParamFilter?: (originalName: string, paramValue: string) => string;
Some applications may need to have some parameter modified before getting those inside the applet.
Example:
cheerpjInit({ appletParamFilter: function (name, value) { if (name === "httpServer") return value.replace("http", "https"); return value; },});
javaProperties

javaProperties?: string[];
An array of Java properties in the form "key=value"
. They will be defined on the System object (System properties). This option should be used if command line arguments in the form -Dkey=value
are required when using native Java.
Example usage:
cheerpjInit({ javaProperties: ["prop1=value1", "prop2=value2"] });
tailscaleControlUrl

tailscaleControlUrl?: string;
This option expects a string URL of the Tailscale control plane. The control plane verifies the user’s identity and validates various keys among the connected peers in the network. Only pass this option if you are self-hosting Tailscale, if omitted, it will point to the Tailscale control plane.
Example usage:
cheerpjInit({ tailscaleControlUrl: "https://my.url.com/" });
tailscaleDnsUrl

tailscaleDnsUrl?: string;
Expects a string with the IPv4 or IPv6 address to use for DNS queries. If omitted, the default IP address is “8.8.8.8”.
Example usage:
cheerpjInit({ tailscaleDnsUrl: "1.1.1.1" });
tailscaleAuthKey

tailscaleAuthKey?: string;
This option expects a string that contains a Tailscale auth key. Using auth keys allows to register new nodes that are pre-authenticated. You can create an auth key here. This option is mutually exclusive with
tailscaleLoginUrlCb
For more information about auth keys visit the Tailscale auth keys documentation.
Example of usage:
cheerpjInit({ tailscaleAuthKey: "AuthKeyStringGoesHere" });
tailscaleLoginUrlCb

tailscaleLoginUrlCb?: (url: string) => void;
This option is used to login into a Tailscale network and it is mutually exclusive with tailscaleAuthKey
. It expects the base URL of a control server that will continue and finish the login process. This callback is executed when it is time to prompt the user to login to Tailscale via the UI.
For more information visit the Tailscale documentation.
cheerpjInit({ tailscaleLoginUrlCb(url) { // your function code here to continue with login },});
fetch

fetch?: ( url: string, method: string, postData: ArrayBuffer, headers: unknown[], ) => Promise<unknown>;
This option is used to make a fetch
request over the network.