Hello World Applet in Java
David Matuszek, Villanova University

Here is everybody's favorite program in Java, this time as an applet:

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

public class HiWorld extends Applet
{
  public void paint (Graphics g)
  {
    g.drawString ("Hi, world!", 50, 100);
  }
}

This time, however, you need the following additional file:

<html>
<head>
  <title> Hi World Applet </title>
</head>

<body>
  <applet code="HiWorld.class" width=300 height=200></applet>
</body>
</html>

To run this program,

  1. Copy the Java applet exactly as written onto a file named HiWorld.java. Do not change the name of this file.
  2. Copy the HTML onto a file. HiWorld.html is the name used in this example, but it really doesn't matter.
  3. Compile the file by putting it in the same directory as the compiler and issuing the command
        javac HiWorld.java
    This should create a file named HiWorld.class
  4. Run the file either by issuing the command
        appletviewer HiWorld.html
    or by opening the file HiWorld.html in your favorite browser. (Make sure Java is enabled in your browser.)
    This should print "Hi, World!"

Notes