javamacosapplet

Java Applet not running on MacOS


I am trying to learn about Java Applets in VSCode but am not able to get through the intricacies. This is my java code in main.java file:

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

@SuppressWarnings("removal")
public class main extends Applet {
    public void paint(Graphics g) {
        g.drawOval(100, 100, 300, 300);
        g.drawString("Geeky Shows", 230, 250);
    }
}

I type in 2 commands in the terminal to run it.

javac main.java
appletviewer main.java

The terminal loads for a split second but then nothing happens. My java version:

➜  applets git:(main) ✗ java --version
openjdk 21.0.2 2024-01-16 LTS
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode)
➜  applets git:(main) ✗ 

mac arch:

➜  applets git:(main) ✗ uname -m
arm64
➜  applets git:(main) ✗ 

Am I doing something wrong?


Solution

  • Java 8

    Java Applets were deprecated in JDK 9 and removed in JDK 11. You are using JDK 21 which obviously doesn't include applet support.

    If you can install Java 8 then it should work, as mentioned in the comments. Java 8 comes bundled with the Applet Viewer app to run on Applet without a web browser.

    I would talk to your teacher to see if you could use Swing instead as an alternative to obsolete technology abandoned years ago. You easily adapt any Applet source code to run as a Swing app. Swing is supported in all versions of Java including the latest.

    You may be able to obtain an implementation of Java 8 from any of several vendors including Azul Systems, IBM, Red Hat, Amazon, BellSoft, Oracle, Adoptium, Oracle, Microsoft, SAP, and more. Licensing terms vary; study before downloading.

    You can download a Mac installer from one of those vendors. Or, a subset of those vendors make make their JDK products available via SDKMAN! tool. That very convenient tool works well on macOS.