Java Applets in 2023

I’ve explained recently how to display Macromedia Flash movies in 2023, using Ruffle; today we’re going to learn how to run Java Applets in your modern browser of 2023, without having to install Java. Yes, it’s possible.

Enter CheerpJ. It’s an extension for Google Chrome or Microsoft Edge that somehow transpiles *.class files into WebAssembly… which allows a good old applet I wrote in 1997 (!) to render correctly on a “modern” browser of 2023.

To run the calculator, install the extension and then click on it to activate and load the applet. It will appear in the white space between this paragraph and the next. Pretty promise.

This is how the calculator above should look like, in case you’re using Firefox or don’t have the extension installed it doesn’t work:

I wrote it on Windows 3.1 (yes, it was still my OS in 1997) using the first 16-bit implementation of the JDK and JRE ever produced: the IBM Applet Development Kit, released August 1996. Don’t believe me? Here’s an InfoWorld article proving it. A few months later Microsoft released its own 16-bit JVM for Windows 3.1, still a very popular operating system in 1996 and 1997. But I used IBM’s.

I don’t remember what editor I used to write the code, though; most probably Notepad.

Feel free to download the archive with the complete project, with files dated September 17th, 1997 (hint: it was a Wednesday.) Find here the (admittedly terrible) Java code I wrote in 1997 to make this contraption. My apologies. You’ve been warned.

import java.applet.*;
import java.awt.*;

/** Calculator Applet
	@author Adrian Kosmaczewski, (c) 1997
	@version 1.0
*/

public class CalcApplet extends Applet
{
	String CalcType = "";
	CalcMachine calculator = null;
	Label errorLabel = new Label("Missing <PARAM NAME=\"CalcType\" VALUE=\"Standard\" \nor VALUE=\"Scientific\"> tag in CalcApplet.html.");
	private double firstValue, secondValue;
	private char operationChosen = '=';
	private boolean dotNotAlreadyUsed = true;

	public void init()
	{
		CalcType = getParameter("CalcType");
		if(CalcType == null)     // if the HTML page is wrong...
		{
			add(errorLabel);
		}
		else if(CalcType.equals("Standard") || CalcType.equals("Scientific"))
		//  if the HTML page is correct...
		{
			calculator = new CalcMachine(CalcType);
			add(calculator);
		}
	}

// ...

Update, 2024-05-17: If you have Java 8 (or earlier) installed in your system, you can download the archive, expand it, and use the appletviewer CalcApplet.html command to launch the applet locally. For example, the default version of the OpenJDK installed in FreeBSD 14 is 8 (“1.8.0_402”), which means you can execute the code on your system as-is, and it will show up as the image below.

Update, 2024-10-04: Just discovered that one can run CheerpJ without the extension! The calculator should be visible on this page, no matter which browser you’re using.

By the way, I also discovered that one can use TeaVM, Bytecoder (a framework to interpret and transpile JVM bytecode to JavaScript, OpenCL or WebAssembly), or JWebAssembly (a Java bytecode to WebAssembly compiler) to run those old applets of yore on your modern browser.

Update, 2024-11-22: I’ve brought the NeXT clock applet back to life on the GaMMA page, thanks to CheerpJ! Check it out!