Run a Java Applet
Run a java applet in modern browsers
CheerpJ runs Java applets in modern browsers from their existing JAR files, with no Java plug-in and no local Java installation. This page shows how to add CheerpJ to your own page so the applet on it runs in the browser.
The Applet Runner browser extension runs Java applets on the pages they are already on, with no changes to the page.
Running your own applet
To get started you will need:
- Your applet’s JAR file(s).
- The HTML page the applet is embedded in, with its
<applet>,<object>or<embed>tag. - An HTTP server that supports Range requests, to test the page locally.
1. Integrating CheerpJ in your HTML file
Leave the applet tag as it is and add two scripts to the page:
- A
<script>in the<head>of the page that loads the CheerpJ runtime:
<script src="https://cjrtnc.leaningtech.com/5.0/loader.js"></script>- A second script that calls
cheerpjInit()to initialise the CheerpJ runtime environment. CheerpJ then finds the applet tags on the page and runs the applets in them.
<script> cheerpjInit();</script>For example:
<!doctype html><html lang="en"> <head> <meta charset="utf-8" /> <title>CheerpJ Applet Example</title> <script src="https://cjrtnc.leaningtech.com/5.0/loader.js"></script> </head> <body> <applet archive="Example.jar" code="ExamplePath.ExampleApplet" height="900" width="900" ></applet> <script> cheerpjInit(); </script> </body></html>The archive and code attributes, and any <param> elements inside the tag, work as they do with the Java plug-in. The archive path is relative to the page, so it does not need the /app/ prefix.
Java applets and Java 8Applets run on Java 8 only. cheerpjInit() uses Java 8 when no version is given, so do not set the version option on a page with applets.
If some of your users still have the native Java plug-in installed, rename the tag with a cheerpj- prefix so that only CheerpJ runs the applet: <cheerpj-applet>, <cheerpj-object> and <cheerpj-embed> are all supported.
2. Host your page locally
Serve the directory that holds the page and the applet’s JAR files with an HTTP server, such as http-server:
npx http-server -p 8080Then open http://localhost:8080 in the browser.
InfoOpening the page directly from disk (a file:// URL) does not work. The server must support HTTP Range requests: npx http-server and npx serve do, Python’s http.server does not. See basic server setup, and use HTTPS in production.
The result
The applet’s area on the page shows CheerpJ’s loading messages, then the applet. The first start downloads the parts of the Java runtime the applet needs, so it is slower than later starts. Depending on the applet and the optimisations applied, this takes a few seconds.
If the applet does not appear, check that the page URL starts with http:// or https://, that the archive path points to the JAR (the browser developer tools’ Network tab shows a 404 if it does not) and that no version other than 8 is passed to cheerpjInit().